farzaneh___
farzaneh___

Reputation: 127

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

Upvotes: -1

Views: 1191

Answers (2)

farzaneh___
farzaneh___

Reputation: 127

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.

Upvotes: 0

Saeed Arashfar
Saeed Arashfar

Reputation: 1

public class RegisterController : Controller
{
    public ActionResult Account()
    {
        return View();
    }
}
routes.MapRoute(
    name: "Register",
    url: "Reg/{id}",
    defaults: new { controller = "Register", action = "Account", id = UrlParameter.Optional }
);
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Upvotes: 0

Related Questions