2

所以,假设我有以下部分观点:

<td>
    @Html.ActionLink("Delete")
</td>

我只希望用户拥有特定权限时才可以使用它 - DeleteItems. 执行此操作的几种方法是使用if语句包装链接或为ActionLink接受一组权限的帮助程序进行重载。

If版本:

@if (User.HasPermission(Permissions.DeleteItems))
{
<td>
    @Html.ActionLink("Delete", "Delete")
</td>
}

重载版本:

<td>
    @Html.ActionLink("Delete", "Delete", Permissions.DeleteItems)
</td>

酷 - 我认为这里明显的赢家是超载。

但是,如果我要屏蔽的区域更大怎么办?

<div class="deleteWrapper">
    <table>
        <tr>
            <td>Id</td>
            <td></td>
        </tr>
        @foreach (var item in items)
        {
            <tr>
                <td>@item.Id</td>
                <td>@Html.ActionLink("Delete", "Delete", new { id = @item.Id })
            </tr>
        }
    </table>
</div>

当然,该If版本仍然有效,但我不想@if (User.Has...(...))在我的观点中无处不在。我喜欢的是以下语法Html.BeginForm

@using(Html.BeginForm(...))
{
    ...
}

但是,BeginForm 只会增加响应- 它实际上并没有对括号之间的视图做任何事情。有没有办法使用助手删除括号内的标记?

@using(Html.ShowIf(...))
{
    ...
}
4

1 回答 1

3

是的你可以。这是一个仅适用于的示例Razor

@using (this.BeginPermissionScope(Permissions.DeleteItems))
{
    using (Html.BeginForm("Delete", "Home"))
    {
        <input type="submit" value="Submit" />
    }
}

C#代码:

public static class HtmlHelperPermissionScopeExtensions
{
    /// <summary>
    /// Begin new rendering scope based on current user permissions
    /// </summary>
    public static IDisposable BeginPermissionScope(this WebViewPage webViewPage, Permissions permission)
    {
        var currentIdentity = webViewPage.User.Identity;
        bool userHasPermission = currentIdentity.HasPermission(permission);
        return new PermissionScopeHelper(webViewPage, !userHasPermission);
    }

    /// <summary>
    /// Helper class that replaces current output stream with its own
    /// </summary>
    private sealed class PermissionScopeHelper : IDisposable
    {
        WebViewPage webViewPage;
        TextWriter oldWriter;
        TextWriter newWriter;

        /// <summary>
        /// Initializes helper class instance. If ignoreMarkupUntilDispose is set to true, then 
        /// all markup writen to response will be ignored
        /// </summary>
        public PermissionScopeHelper(WebViewPage page, bool ignoreMarkupUntilDispose)
        {
            if (ignoreMarkupUntilDispose)
            {
                webViewPage = page;
                newWriter = new StringWriter();

                // Replace output TextWriter for Write() and WriteLiteral() methods
                webViewPage.OutputStack.Push(newWriter);

                // Replace output TextWriter for all HtmlHelper and AjaxHelper extensions
                oldWriter = webViewPage.ViewContext.Writer;
                webViewPage.ViewContext.Writer = newWriter;
            }
        }

        /// <summary>
        /// Restore output TextWriters
        /// </summary>
        public void Dispose()
        {
            if (webViewPage != null)
            {
                webViewPage.ViewContext.Writer = oldWriter;
                webViewPage.OutputStack.Pop();
                newWriter.Dispose();
            }
        }
    }

}
于 2013-09-26T14:17:51.437 回答