加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 基于MVC角色的路由

发布时间:2020-12-16 00:31:39 所属栏目:asp.Net 来源:网络整理
导读:我有一个项目有2个区域/ Admin和/ User。 Admin的默认路由为/ Admin / Home / Index,用户的默认路由为/ User / Home / Index。 是否可以实现路由以使其家乡网址看起来像/ Profile / Index,但是要显示/ Admin / Home / Index的用户管理员和/ User / Home /
我有一个项目有2个区域/ Admin和/ User。

Admin的默认路由为/ Admin / Home / Index,用户的默认路由为/ User / Home / Index。

是否可以实现路由以使其家乡网址看起来像/ Profile / Index,但是要显示/ Admin / Home / Index的用户管理员和/ User / Home / Index的内容?

UPD

最后找出如何做到这一点

context.MapRoute(
    "Admin","Profile/{action}",new { area = AreaName,controller = "Home",action = "Index" },new { RoleConstraint = new Core.RoleConstraint() },new[] { "MvcApplication1.Areas.Admin.Controllers" }
);
...
context.MapRoute(
    "User",new[] { "MvcApplication1.Areas.User.Controllers" }
);

public class RoleConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext,Route route,string parameterName,RouteValueDictionary values,RouteDirection routeDirection)
    {
        string roleName = db.GetRoleByUserName(httpContext.User.Identity.Name);
        string areaName = route.Defaults["area"].ToString();
        return areaName == roleName;
    }
}

它的作品,但对我来说,这不是MVC的方式。有人知道怎么做吗?

解决方法

是。您显示的示例非常接近许多Microsoft提供的使用路由约束的示例。在将请求传递到控件之前,路由引擎充当预代理(或路由器,如果您愿意)。定义了像IRouteConstraint这样的项目,所以你可以做你所描述的内容。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读