Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否可以在MVC中生成的所有URL上保留自定义URL前缀?_C#_Asp.net Mvc_Asp.net Mvc 4_Asp.net Mvc Routing - Fatal编程技术网

C# 是否可以在MVC中生成的所有URL上保留自定义URL前缀?

C# 是否可以在MVC中生成的所有URL上保留自定义URL前缀?,c#,asp.net-mvc,asp.net-mvc-4,asp.net-mvc-routing,C#,Asp.net Mvc,Asp.net Mvc 4,Asp.net Mvc Routing,我想在所有MVC生成的Url上保留一些自定义Url上下文。这就是我的情况:多个角色的用户通过其角色(即…../admin/{controller}/..)的前缀URL访问我的网站。这是我的项目的一个要求,必须是这样。我希望在以后在视图、控件等中生成的每个url中保留用户角色上下文(方法如Html.ActionLink、url.Action、RedirectTo等) RouteConfig.cs中的我的路线定义: // guest route routes.MapRoute( name:

我想在所有MVC生成的Url上保留一些自定义Url上下文。这就是我的情况:多个角色的用户通过其角色(即…../admin/{controller}/..)的前缀URL访问我的网站。这是我的项目的一个要求,必须是这样。我希望在以后在视图、控件等中生成的每个url中保留用户角色上下文(方法如Html.ActionLink、url.Action、RedirectTo等)

RouteConfig.cs中的我的路线定义:

// guest route
routes.MapRoute(
    name: "Guest",
    url: "guest/{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, userContext = UserRoles.Guest }
);

// authentized user
routes.MapRoute(
    name: "User",
    url: "user/{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, userContext = UserRoles.User }
);

// authentized admin 
routes.MapRoute(
    name: "Admin",
    url: "admin/{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, userContext = UserRoles.Admin }
);
然后在BaseController中(每个其他控制器都从该控制器继承):

因此,我在每个控制器/操作和视图中都有
CurrentRole
属性(将CurrentRole指定给ViewBag)。我可以通过使用方法将routeValue
new{userContext=CurrentRole}
添加到url来创建上下文url

但是!!!我已经准备好在没有用户上下文的情况下创建这个网站,不想重写像这样的方法的每次使用,也不想创建自己的自定义(Html,Url)扩展来覆盖Url使用方法的每次使用。我想创建一些对
Url.GenerateUrl()
的重写,或者其他可以保留我的用户上下文的东西

routes.MapRoute(
    name: "WithUserContext",
    url: "{usercontext}/{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    constraints: new { usercontext = "guest|user|admin" }
);
你有什么想法吗


非常感谢

更改此路径,以便Html.ActionLink、Url.Action、RedirectTo将使用当前用户上下文

routes.MapRoute(
    name: "WithUserContext",
    url: "{usercontext}/{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    constraints: new { usercontext = "guest|user|admin" }
);

谢谢,这很有帮助。但我必须在OnActionExecuting方法中的基本控制器中打开RouteData.Values[“userContext”],以将一些UserRoles enum分配给受保护的CurrentRole。在这个路由中,有没有办法将UserRoles枚举中的内容分配给userContext而不是字符串?您能详细说明一下将UserRoles枚举中的内容分配给userContext是什么意思吗?不幸的是,我不明白。我已经定义了enum UserRoles,其中包含用户可以实现的角色。我用它来回答我的问题。之后是RouteData.Values[“userContext”]从UserRoles枚举设置为一个角色。。。但这已经不需要了,我喜欢你解决这个问题的方式。再次感谢你