Search code examples
asp.net-mvcurlroutesasp.net-mvc-routingrouteconfig

Change url in Route Config in Mvc


I want to change the address of a function (via RegisterRoutes). I defined two routes.MapRoute but not working any. I checked many examples and matched my code with them, but the problem still remains. The real address is: http://localhost:3127/account/register/3

I want my address to be changed to the following address: http://localhost:3127/Reg

Should [Route("Reg")] be used at the top of the function for this? Is a redirect required for this or not? Do I need web.config settings to do this?

my action is:

[AllowAnonymous]
    [Route("Reg")]
    public virtual ActionResult Register()
    {
        return View();
    }

my configuration in Register Routes:

   routes.MapRoute(
         name: "Register",
         url: "Reg",
         defaults: new { controller = "Account", action = "Register" },
         namespaces: new[] { "WebSite.Controllers" }
     );
        routes.MapRoute(
          name: "Account",
          url: "Account/Reg",
         defaults: new { controller = "Account", action = "Register" },
         namespaces: new[] { "WebSite.Controllers" });

routeconfig

error of call url address


Solution

  • I resolve this problem. I install IIS and change my code to below code. Then my address became OK.

     routes.MapRoute(
             name: "Reg",
             url: "Reg",
             defaults: new { controller = "Account", action = "Register" },
             namespaces: new[] { "WebSite.Controllers" }
         );
    
    [Route("Reg")]
        public virtual ActionResult Register()
        {
            return View();
        }
    

    Needless to say, I put this piece of code at the beginning of the file RouteConfig before all cases and after routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    Now, my url is http://localhost:3127/Reg everywhere.