0
votes

How to Redirect url in route config in Asp.net Mvc...

My controller is "Home" and action is "Categories"

My url is localhost:49606/Home/Categories

now i changed using routes below

routes.MapRoute(
            name: "Categories",
            url: "Categories",
            defaults: new { controller = "Home", 
                            action = "Categories", 
                            id = UrlParameter.Optional }
        );

New url is localhost:49606/Categories

But still localhost:49606/Home/Categories url is avilabe...

I want to redirect localhost:49606/Home/Categories to localhost:49606/Categories.

or

Hide url localhost:49606/Home/Categories.

Please suggest me good way to redirect url or hide? I want to redirect url in route config file in asp.net mvc 5

1
Please show us the whole route config, because the order in which you define your routes is important.Marco

1 Answers

1
votes

You can use Attribute routing. In case of using attribute routing,you need to allow attribute routing to map your routes by adding following line in RouteConfig.cs

  routes.MapMvcAttributeRoutes();

And then you can specify your route what you want. no matter what your action name is.

    [Route("Categories")]
    public ActionResult GetCategories() 
    {
         return View();
    }