c# - ASP.NET MVC service route overrides default route -
i have added wcf service mvc 5 application, , created route it:
public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.add(new serviceroute("service1.svc", new servicehostfactory(), typeof(service1))); routes.maproute( name: "default", url: "{controller}/{action}/{id}", defaults: new { controller = "home", action = "index", id = urlparameter.optional } ); }
the problem links leads service1.svc route now. @html.actionlink("passport maker", "index", "home", new { area = "" }, new { @class = "navbar-brand" })
become http://localhost:50099/service1.svc?action=index&controller=home
, other links change in same way.
if add serviceroute after "default" route, links work correctly service unavailable.
why happens (there no "service1" in links, why select service route then?) , how fix it?
the solution:
routes.maproute( name: "default", url: "{controller}/{action}/{id}", defaults: new { controller = "home", action = "index", id = urlparameter.optional }, constraints: new { controller = "^(?!service1.svc).*" } ); routes.add(new serviceroute("service1.svc", new servicehostfactory(), typeof(service1)));
explanations may encounter similar problem: reason of problem html.actionlink
uses first matching route generate link. , service route first , matching, because route not require include {controller}
, {action}
parameters matched (as thought initially).
the solution put default route first, used html.actionlink
. , still able use service route, need exclude the first route using constraints. regex ^(?!service1.svc).*
matches controller names don't start "service1.svc".
Comments
Post a Comment