asp.net mvc html扩展,asp.net MVC 4 - Htmlhelper扩展(asp.net MVC 4 - Htmlhelper extensions)

asp.net MVC 4 - Htmlhelper扩展(asp.net MVC 4 - Htmlhelper extensions)

我正在为Html.RenderAction创建一个自定义的HtmlHelper扩展。 我的父视图将包含许多不同的部分视图,这些视图将通过调用Html.Renderaction来呈现。 但是管理员可以为某个角色排序部分视图的切换,或者他可以完全停用整个应用程序的动作所以我打算为Html.RenderAction设置一个扩展方法,然后检查角色并查看是否角色可以访问特定的操作。 这个角色到动作的映射在xml中是有用的,我打算只在内存数据结构中加载一次这个xml一次。 并让html助手扩展查看该数据结构。 这是一个好方法吗? 有更好的方法吗?

@section column2 {

@{Html.RenderActionIfIfAllowed("DashboardItem_Users", "DashBoard",User);}

}

@section column3 {

@{Html.RenderActionIfIfAllowed("DashboardItem_Orders", "DashBoard", User);}

}

我必须渲染上面的偏见。 所以我创建了一个名为Html.RenderActionIfIfAllowed的html助手扩展。

public static class HtmlHelperExtensions

{

public static void RenderActionIfIfAllowed(this HtmlHelper htmlHelper, string actionName, string controllerName, IPrincipal user)

{

//We can use the layour manager class to check if a particular role has access to an action and also if the action is active.

//Hard coding here just for demo purpose

if (user.IsInRole("Admin") && actionName != "DashboardItem_Users")

{

System.Web.Mvc.Html.ChildActionExtensions.RenderAction(htmlHelper, actionName, controllerName);

}

else if (user.Identity.IsAuthenticated && !user.IsInRole("Admin"))

{

System.Web.Mvc.Html.ChildActionExtensions.RenderAction(htmlHelper, actionName, controllerName);

}

}

}

这样做的原因是因为我们希望根据视图是否处于活动状态来动态显示或不显示用户的特定视图。 我们将读取一个xml文件,该文件将说明视图是否处于活动状态而不是用户并相应地呈现它

I am creating a custom HtmlHelper extension for Html.RenderAction. My Parent view will contain many different partial views which will be rendered byt calling Html.Renderaction. But the the admin can sort off switch of a partial view for a role or he can completely deactivate the action for entire application So i am planning to have an extension method for Html.RenderAction which will in turn check for the role and see if the role has access to a particular action. This role to action mapping is dine in xml and I am planning to load this xml as in memory data structure only once. And have the html helper extension look into that data structure. Is that a good way to go? Any better ways?

@section column2 {

@{Html.RenderActionIfIfAllowed("DashboardItem_Users", "DashBoard",User);}

}

@section column3 {

@{Html.RenderActionIfIfAllowed("DashboardItem_Orders", "DashBoard", User);}

}

I have to render the above partialviews. So i have created a html helper extension called Html.RenderActionIfIfAllowed.

public static class HtmlHelperExtensions

{

public static void RenderActionIfIfAllowed(this HtmlHelper htmlHelper, string actionName, string controllerName, IPrincipal user)

{

//We can use the layour manager class to check if a particular role has access to an action and also if the action is active.

//Hard coding here just for demo purpose

if (user.IsInRole("Admin") && actionName != "DashboardItem_Users")

{

System.Web.Mvc.Html.ChildActionExtensions.RenderAction(htmlHelper, actionName, controllerName);

}

else if (user.Identity.IsAuthenticated && !user.IsInRole("Admin"))

{

System.Web.Mvc.Html.ChildActionExtensions.RenderAction(htmlHelper, actionName, controllerName);

}

}

}

The reason for doing that way is because we would like to dynamically show or not show a aprtial view to user based on whether the view is active or not. We will read an xml file that will say whether the view is active not for a user and render it accordingly

原文:https://stackoverflow.com/questions/15080356

更新时间:2019-12-08 01:57

最满意答案

我曾经为此创建ViewModel并设置bool属性

public class DashBoardViewModel{

public DashBoard dashBoard{get;set;}

bool showItemDashBoard{get;set;}

bool showOrderDashBoard{get;set;}

}

在Controller中,我验证用户角色并设置这些布尔属性。

在视图中

if(Model.showItemDashBoard){

@Html.RenderAction("Action","Controller")

}

I used to create ViewModel for this and set bool properties

public class DashBoardViewModel{

public DashBoard dashBoard{get;set;}

bool showItemDashBoard{get;set;}

bool showOrderDashBoard{get;set;}

}

In Controller I validate user role and set those boolean properties.

In View

if(Model.showItemDashBoard){

@Html.RenderAction("Action","Controller")

}

相关问答

您应该检查Codeplex上的MVC-Contrib项目... 此外,我只是发现这个分页扩展到HtmlHelper,看起来很酷,虽然我还没有使用它。 You should check the MVC-Contrib project on Codeplex... Also, I just found this paging extension to the HtmlHelper which looks pretty cool, although I've not used it yet.

还有一个额外的参数需要TextWriter : var viewContext = new ViewContext(

context,

new WebFormView("HtmlHelperView"),

new ViewDataDictionary(),

new TempDataDictionary(),

context.HttpContext.Response.Output

);

这里的问题是为什么你需要自己实例化htmlHelper而不是使用视图中提

...

我曾经为此创建ViewModel并设置bool属性 public class DashBoardViewModel{

public DashBoard dashBoard{get;set;}

bool showItemDashBoard{get;set;}

bool showOrderDashBoard{get;set;}

}

在Controller中,我验证用户角色并设置这些布尔属性。 在视图中 if(Model.showItemDashBoard){

@Html.RenderAc

...

HtmlHelper由ASP.NET MVC在内部实例化 - 这通常不需要担心。 实际上它实例化的位置取决于您使用它的位置。 它实例化的主要地方是ViewPage的InitHelpers()方法。 在ViewUserControls中,它在Html属性的getter中按需创建。 在ViewMasterPages中,它只使用ViewPage的Html属性。 The HtmlHelper is instantiated internally by ASP.NET MVC - it's not some

...

您应该从HtmlHelper扩展方法返回一个IHtmlString ,以便输出不是HTML编码的,因为它是不应该再次编码的HTML标记。 例如 public static IHtmlString HaidarImage(this HtmlHelper instance, string src)

{

TagBuilder inst = new TagBuilder("img");

inst.MergeAttribute("src", src);

return new Htm

...

这里有一个这样的帮助器的例子。 我发现它非常有用。 Here you have an example of such a helper. I found it very useful.

您是否在web.config中添加了对MvcBookApplication命名空间的引用? Have you added a reference to MvcBookApplication namespace in your web.config ?

让ASP.NET MVC知道服务器端包装器所在的Kendo.Mvc.UI命名空间。 为此,请更新Web应用程序的web.config文件。 步骤1如果使用ASPX,请打开Views / Web.config或root Web.config。 步骤2找到名称空间标签。 第3步将添加标签添加到名称空间标签。 例

...

HTML帮助器仍然是ASP.NET Core中的一件事。 仅仅因为标记帮助程序是呈现自定义HTML的新且通常更灵活的解决方案,并不意味着HTML帮助程序已经消失或者它们没有剩下的用处。 内置标记帮助程序实际上基于HTML帮助程序,并将使用相同的内部后端来生成输出。 所以它只是同一个东西的不同界面。 这就是说,由于ASP.NET Core呈现视图的方式,与标记助手(它是一个非常普遍的功能)的工作方式相比,捕获using块内的内容要困难得多。 我已经坐了一段时间,并提出以下几点。 这可以通过在块打开的

...

您需要包含System.Web.Mvc.Html命名空间,因为大多数HtmlHelper方法实际上都是在该命名空间中的类中定义的扩展。 您可以在www.codeplex.com/aspnet找到RC1(也可能是RC2源代码)。 点击源代码标签并导航到MVC源代码树。 You need to include the System.Web.Mvc.Html namespace since most of the HtmlHelper methods are really extensions defi

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值