Monday, 15 September 2014

c# - ASP.NET MVC service route overrides default route -


I added WCF service to MVC 5 application, and created a path for it:

  Public static zero register routes {routes.IgnoreRoute ("{resource} .axd / {* pathInfo}"); way. Add (New Service Way ("Service 1 SVC", New ServiceHostFactory (), Typef (Service1)); Routes.MapRoute (name: "default", url: "{controller} / {action} / {id}", default: new {controller = "home", action = "index", id = urlparameter.option}}; }  

The problem is that all my links lead to the Service1.svc route. @ Html.ActionLink ("Passport Maker", "Index", "Home", New {domain = "}, New {@class =" navbar-brand "}) Become http: // localhost: 50099 / service 1.svc? Action = index and controller = home and other links change in the same way.

If I add the service path after the "default" route, The link works well but the service is unavailable.

Why this happens (the link does not contain "Service 1", so why do they choose the service path?)

solution:

  routes.MapRoute (name New {controller = "home", action = "index", id = UrlParameter.Optional}, barriers: new {controller = "url": "default", url: "{controller} / {action} / {id} "^ (?! Service1.svc). *"}); Add route (New service route ("Service 1 SVC", New ServiceHostFactory (), Typef (Service1));  

Explanation for those who may face a similar problem: The reason for the problem was that Html.ActionLink Uses the first match path to generate links. And my service path was first and was matched, because in a path there is no need to match the {controller} and {action} parameters (as I Initially thought).

The solution is to put the default route first, so it is used by Html.ActionLink . And to still be able to use the service path, it needs to be separated from the first route using the constraints. Regex ^ (?! Service1.svc). * matches those controller names that do not start with "Service1.svc".


No comments:

Post a Comment