skip to Main Content

I am using Asp.net Mvc 5 with C#.

I want to disable default routing in my project. My map routes like;

    routes.MapRoute(name: "News",
                    url: "haberler",
                    defaults: new { controller = "News", action = "Index"});


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

When a user visits my website’s news page, it’s like;

www.domain.com/haberler 

But also the user can visit the news page as below;

www.domain.com/news

I want to remove that "/news" or direct to seo-friendly url like; “/haberler”

So how can I disable default routing (Controller-Name-Convension) routing?

3

Answers


  1. Instead of removing the “default” route, you can add a controller constraint to it

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        constraints: new { controller = @"(Account|Manage|Home)" }                
    );
    

    Like this, /news will return 404 Not Found.

    Login or Signup to reply.
  2. www.domain.com/news will not match in any of your routes hence it will throw an error.

    Login or Signup to reply.
  3. Try removing with this

    RouteTable.Routes.Remove(RouteTable.Routes["NAME ROUTE YOU WISH TO RMOVE"]);
    

    Hope this will helps you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search