[英]Map Custom Route ASP.NET MVC5
I haven't used .NET Routing before.我以前没有使用过 .NET 路由。 I have a URL: http://myurl.com/Account/Login/?IsIPA=true
.我有一个 URL: http://myurl.com/Account/Login/?IsIPA=true
。 I want to be able to hit this URL with the following: http://myurl.com/IPA
我希望能够使用以下内容访问此 URL: http://myurl.com/IPA
: http://myurl.com/IPA
This is the only custom route I want hit.这是我想要命中的唯一自定义路线。
Can I create a route just for a single URL like this?我可以为这样的单个 URL 创建路由吗?
My code that isn't working is:我不起作用的代码是:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute("IPA", "Account/Login/{IsIPA}", new { controller = "Account", action = "Login", IsIPA = "true" });
}
I get the error:我收到错误:
The constraint entry
IsIPA
on the route with route templateAccount/Login/{IsIPA}=True
must have a string value or be of a type which implementsSystem.Web.Routing.IRouteConstraint
.具有路由模板Account/Login/{IsIPA}=True
的路由上的约束条目IsIPA
必须具有字符串值或实现System.Web.Routing.IRouteConstraint
的类型。
Route matching is similar to a switch case statement.路由匹配类似于 switch case 语句。 The url
parameter and any default values and constraints are all considered to determine whether or not it is a match with the incoming URL. url
参数和任何默认值和约束都被考虑来确定它是否与传入的 URL 匹配。 If the route matches, it will then create a dictionary of route values based on the configuration.如果路由匹配,它将根据配置创建一个路由值字典。 If the route does not match, the next route in the collection is tried until a match is found (or not).如果路由不匹配,则尝试集合中的下一个路由,直到找到(或不匹配)。
This means the order that routes are specified is important.这意味着指定路由的顺序很重要。 The default route matches any URL with 0, 1, 2, or 3 segments.默认路由匹配具有 0、1、2 或 3 个段的任何URL。 Therefore, in most cases you will need to define your custom route before the default route.因此,在大多数情况下,您需要在默认路由之前定义自定义路由。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "IPA",
url: "IPA",
defaults: new { controller = "Account", action = "Login", IsIPA = "true" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
The above configuration will route http://myurl.com/IPA
to the Controller named Account
and Action method named Login
, and pass the additional route key IsIPA
.上面的配置将http://myurl.com/IPA
路由到名为Account
的 Controller 和名为Login
Action 方法,并传递额外的路由键IsIPA
。 This same URL will be built for the Controller/Action/IsIPA combination because it is the first one that matches in the list.将为 Controller/Action/IsIPA 组合构建相同的 URL,因为它是列表中第一个匹配的 URL。
Note that the original URL http://myurl.com/Account/Login/?IsIPA=true
will still work and still route to the same location.请注意,原始 URL http://myurl.com/Account/Login/?IsIPA=true
仍然有效并且仍然路由到相同的位置。 This configuration just adds an extra route to that resource.此配置只是为该资源添加了一个额外的路由。
Without testing it, I think that you want this:没有测试它,我认为你想要这个:
routes.MapRoute("IPA", "Account/Login/{IsIPA}",
new { controller = "Account", action = "Login", IsIPA = "true"});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.