BahaiResearch.com BahaiResearch.com - 11 days ago 6
ASP.NET (C#) Question

MVC Route with an area

In MVC 5, I have an old controller "MyOld" and need it to point to "MyNew", but the request keeps going to MyOld.

routes.MapRoute(
name: "newthing",
url: "Trade/MyOld",
defaults: new { controller = "MyNew", action = "Index", area = "Trade" }
).DataTokens.Add("area","Trade");

Answer

You should add Router to method RegisterArea:

TradeAreaRegistration.cs

public class TradeAreaRegistration : AreaRegistration 
{
    public override string AreaName 
        {
            get 
            {
                return "Trade";
            }
        }

    public override void RegisterArea(AreaRegistrationContext context) 
    {    
        context.MapRoute(
         name: "MyOldToMyNew",
         url: "Trade/MyOld",
         defaults: new { controller = "MyNew", action = "Index", id = UrlParameter.Optional }
     );

        context.MapRoute(
            name: "Trade_default",
            url: "Trade/{controller}/{action}/{id}",
            defaults: new { action = "Index", id = UrlParameter.Optional }
        );
    }
}