I am new to MVC and editing an existing application. Currently, I see the following in RouteConfig.cs:
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Util", "util/{action}", new {controller = "util"}); routes.MapRoute( "Catchall", "{*url}", new {controller = "Main", action = "CatchUrl"}); } }
Inside the main controller, there is logic that basically executes RedirectToRoute and sets the scope, controller, action, and querystring called location to a specific value.
public class MainController : Controller { public ActionResult CatchUrl() { var agencyId = 9; var routeValues = new RouteValueDictionary { {"area", "area1"}, {"controller", "dashboard"}, {"action", "index"}, {"location", "testLocation"} }; return RedirectToRoute(routeValues ); } }
This seems to work fine when you give it an invalid area, it goes over to the standard ones correctly.
I also see the CustomAreaRegistration.cs file:
public abstract class CustomAreaRegistration : AreaRegistration { public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( AreaName + "Ajax", AreaName + "/{controller}/{action}", new { action = "Index" } ); context.MapRoute( AreaName + "ShortUrl", AreaName + "/{controller}", new {action = "Index"} ); } }
I am having trouble understanding how Area routing works and how it knows how to get to the right controller.
Also, I'm trying to get it so that when visiting
/{area}/ it executes some logic and returns you to the correct controller. How CatchUrl Works
My attempt:
routes.MapRoute( "AreaIndex", "{module}/", new {controller = "Main", action = "Index"});
MainController:
public class MainController : Controller { public ActionResult Index() { var requestHost = HttpContext.Request.Url?.Host; var location= requestHost == "localhost" ? Request.QueryString["location"] : requestHost?.Split('.')[0]; var routeValues = new RouteValueDictionary { {"area", ControllerContext.RouteData.Values["module"]}, {"controller", "dashboard"}, {"action", "index"}, {"location", location} }; return RedirectToRoute(routeValues ); } public ActionResult CatchUrl() { var routeValues = new RouteValueDictionary { {"area", "area1"}, {"controller", "dashboard"}, {"action", "index"}, {"location", "testLocation"} }; return RedirectToRoute(routeValues ); } }
And I get
No route in the route table matches the specified values.
I'm not sure why CatchUrl works, but mine doesn't.