简体   繁体   English

没有控制器名称的MVC多个默认路由

[英]MVC multiple Default Routes without controller name

I have got two controllers, one is called Dashboard and the other is called DashboardCash. 我有两个控制器,一个叫做Dashboard,另一个叫做DashboardCash。 Now my application can be accessed by two types of users, one who can only access Dashboard (Type A users) while others can only access DashboardCash (Type B users). 现在,我的应用程序可以由两种类型的用户访问,一种只能访问Dashboard(类型A用户),而另一种只能访问DashboardCash(类型B用户)。 In order to ensure that I have put a login page. 为了确保我已经放置了登录页面。

What I want to do is when Type A users login successfully, I want to show them url with no controller name like http://example.com rather than showing with the controller name such as http://www.example.com/Dashboard . 我要做的是当Type A用户成功登录时,我想给他们显示不带控制器名称的url,例如http://example.com而不是显示带有控制器名称的http://www.example.com/Dashboard And with Type B users I want to show them the same http://www.example.com but here I am replacing DashboardCash. 对于B型用户,我想向他们显示相同的http://www.example.com但是在这里我要替换DashboardCash。

Currently I have this mapping code defined in Global.asax file: 当前,我在Global.asax文件中定义了以下映射代码:

routes.MapRoute(
     "Default", // Route name
     "{controller}/{action}/{id}", // URL with parameters
     new { controller = "Dashboard", action = "Index", id = UrlParameter.Optional
     }, // Parameter defaults
     new string[] { "Merit.Traveller.BMS.Controllers" });

This code works fine for Dashboard now I want to do the same thing for DashboardCash. 这段代码对于Dashboard正常工作,现在我想对DashboardCash做同样的事情。

Write a custom route constraint that makes the route match based on the user type. 编写一个自定义路由约束,使路由根据用户类型进行匹配。

Implement the following interface 实现以下接口

public interface IRouteConstraint
{
     bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection);
}

Then use it in the route, something like this: 然后在路由中使用它,如下所示:

routes.MapRoute(name: "newRoute1",
  url: "{controller}/{action}/{id}",
  defaults: new { controller = "Dashboard", action = "Index" },
  constraints: new { name = new UserTypeARouteConstraint() }
);

EDIT - based on your question below here are more details 编辑-根据下面的问题,这里有更多详细信息

This is what your second route looks like 这就是你的第二条路线

routes.MapRoute(name: "newRoute2",
  url: "{controller}/{action}/{id}",
  defaults: new { controller = "DashboardCash", action = "Index" },
  constraints: new { name = new UserTypeBRouteConstraint() }
);

And this is what a constraint looks like 这就是约束的样子

public class UserTypeARouteConstraint : IRouteConstraint
{
     bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
     {
         return IsUserOfTypeA(httpContext);
     }

     private bool IsUserOfTypeA(HttpContextbase httpContext)
     {
         // custom logic to figure out the user group
     }
}

Thanks Yishai, 谢谢伊斋

That fixed the issue, I just added one more thing which is EliminateController as the constraint, which allows the default route to run only if we dont have the dashboard pages. 解决了这个问题,我只添加了另外一件事,即EliminateController作为约束,它仅在没有仪表板页面的情况下才允许默认路由运行。

Here is the detailed code: 这是详细的代码:

http://jsfiddle.net/hf7wexkk/ For Code http://jsfiddle.net/hf7wexkk/ For Code

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM