ASP.Net MVC @ Url.Action () routing to the wrong controller

I am new to MVC. I could integrate MVC 5.2 into my existing web forms Visual Studio 2012 Update 4. I created my first controller and everything worked as expected. Even when I was accessing MVC viewing, I was able to use Windows Form Authentication from my existing project. But when my second controller was created, it started to go bad.

This is my route mapping:

public static class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.EnableFriendlyUrls(); routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } 

I have two controllers located in ~ / controllers. My first controller:

 public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { //return View(); return Redirect("~/Default.aspx"); } public ActionResult CloseSession() { return Redirect("http://www.yahoo.com"); } } 

Second controller:

 public class CajaWebController : Controller { // // GET: /CajaWeb/ public ActionResult Index() { return View(); } public ActionResult CloseSession() { return Redirect("http://www.cnn.com"); } } 

I do not know if this relates to the problem, but I will tell you how the MVC representation is achieved. My VS2012 Launch URL

 http://localhost/Fortia/CajaWeb. 

Fortia is my application name. Since I declared web form validation and

 <location path="CajaWeb"> <system.web> <authorization> <allow roles="Fortia" /> <deny users="*" /> </authorization> </system.web> </location> 

when the debugging of the old WebForms application authentication mechanism starts, the old WebForms login page is called and after a successful login, finally my CajaWebController, Index () action is called. Before creating the CajaWebController, the HomeController was called, but I assume that MVC now displays the correct CajaWeb controller due to the destination URL

 http://localhost/Fortia/CajaWeb. 

The called view contains the following code:

 <a href='@Url.Action("CloseSession", "CajaWeb")'>Close session</a> 

The problem is that when I click on the generated link, MVC calls the HomeController.Index () action, despite the fact that I explicitly set CajaWebController.CloseSession () to @ Url.Action ...

I looked at the generated link and it looks wrong:

 <a href='/Fortia/__FriendlyUrls_SwitchView?action=CloseSession&amp;controller=CajaWeb'> 

he encoded the parameter separator and entered. But anyway I tried handcoding href like

 http://localhost/Fortia/__FriendlyUrls_SwitchView?action=CloseSession&controller=CajaWeb 

but the result was the same.

What's wrong?

+6
source share
2 answers

It would seem that the ASP.NET Friendly Urls package you are using interferes with URL generation. The library seems to be intended for WebForms anyway

If it works without it, then leave it as the MVC URLs are already optimized enough for SEO when the names of the controllers and actions make sense for their content.

0
source

I think the problem is that the route

http://localhost/Fortia/CajaWeb

does not match any routes, so it goes to the default route specified in RouteConfig. You need to set up a route or create an area in your application called "Fortia".

0
source

Source: https://habr.com/ru/post/974425/


All Articles