条件 ASP.NET MVC 3 路由(带区域)
胡罗。
我试图根据当前用户是否是管理员来实现一些条件路由。系统只有两种模式,管理员或非管理员,仅此而已。我使用区域作为我的管理区域,因为控制器名称是相同的,但它们在每种情况下几乎都会提供不同的功能。
然而,在这个系统中,管理员不应该真正知道他们的管理位置,他们只知道他们使用系统来做普通用户所做的事情之外的其他事情。我不希望因此在 URL 方面两者之间有任何区别。我想要做的是能够执行类似 mysite.com/AuditHistory 之类的操作,并且取决于您是管理员还是用户将取决于所使用的控制器。因此,如果是用户发出此请求,那么它将使用常规控制器文件夹中的 AuditHistoryController
,但如果是管理员,那么它将使用区域中的 AuditHistoryController
/管理员/控制器。
我已经看到了 IRouteConstraint
的使用,并且可以按照以下方式执行某些操作:
public class AdminRouteConstraint : IRouteConstraint
{
public AdminRouteConstraint() { }
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return httpContext.User.IsInRole("Admin");
}
}
通过以下操作:
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", controller = "Home", id = UrlParameter.Optional },
new { controller = new AdminRouteConstraint() }
);
我可以简单地去掉前面的“Admin/”并对其他路由执行相同的操作吗但是说UserRouteConstraint
?我还没有在任何地方看到过这样做,并且不确定它是否正确。
关于如何做到这一点有什么想法吗?
Hurro.
I'm trying to achieve some conditional routing based on whether the current user is an admin or not. The system only has two modes, admin or non-admin and nothing more than this. I'm using areas for my admin area because the controller names would be the same, but they'll deliver different functionality pretty much in every case.
In this system, however, the admins shouldn't really be aware of their admin location, they just know that they use the system to do something else other than what regular users do. I don't want there to be any distinction between the two in terms of URL because of this. What I want to do is be able to do something like mysite.com/AuditHistory and dependant on whether you're an admin or user will depend on what controller is used. So if it's a user making this request, then it'd use the AuditHistoryController
in the regular controllers folder, but if it's an admin then it'd use the AuditHistoryController
in Areas/Admin/Controllers.
I've seen the use of IRouteConstraint
and can do something along the following lines:
public class AdminRouteConstraint : IRouteConstraint
{
public AdminRouteConstraint() { }
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return httpContext.User.IsInRole("Admin");
}
}
With the following:
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", controller = "Home", id = UrlParameter.Optional },
new { controller = new AdminRouteConstraint() }
);
Can I simply get rid of "Admin/" at the front and do the same thing for the other routes but say UserRouteConstraint
? I've not seen this done anywhere though and not sure if it's correct.
Any ideas on how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果用户处于某个角色,您能否简单地从
ActionResult
重定向用户?如果您不介意更改 URL 的话?像这样的东西......
对我来说,这有点像黑客。但这可能是一个解决方案。
显然,您需要进行基本检查,例如确保当前请求经过身份验证等。
如果您确实不希望 URL 更改,您可能有两个单独的视图并取消管理区域 code>
事实上,我认为这可能是最干净的解决方案,但可能仍然有点黑客。
我希望这有帮助。
Could you simply redirect the user from the
ActionResult
if they are in a role? That is if you don't mind the URL changing?Something like this...
To me, this is a bit of a hack. But it may be a solution.
Obviously, you would need to do basic checks like making sure the current request is authenticated etc.
If you really don't want the URL to change, you could possibly have two separate views and do away with the admin
Area
In fact I think this is probably the cleanest solution, but is possibly still a bit of a hack.
I hope this helps.