Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC routing not working

My routing is not working properly. I have the following routes defined:

routes.MapRoute(
     name: "CategoryDetails",
     url: "{seoName}",
     defaults: new { controller = "Category", action = "Details" }
);

routes.MapRoute(
     name: "ContactUs",
     url: "contact",
     defaults: new { controller = "Home", action = "Contact" }
);

routes.MapRoute(
     name: "AboutUs",
     url: "about",
     defaults: new { controller = "Home", action = "About" }
);

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

When I click on the about us or contact us links then it takes me to the details action method in the category controller.

This is the markup for my about us and contact us links:

@Html.ActionLink("About", "About", "Home")
@Html.ActionLink("Contact", "Contact", "Home")

My details action method for the category controller:

public ActionResult Details(string seoName)
{
     CategoryViewModel model = categoryTask.Details(seoName);

     return View(model);
}

What is wrong with my route configuration?

like image 766
Brendan Vogt Avatar asked Jan 27 '14 09:01

Brendan Vogt


3 Answers

Reorder your routes from most specific to less specific. That way the routes for contact and about will come before the seoName route:

routes.MapRoute(
        name: "ContactUs",
        url: "contact",
        defaults: new { controller = "Home", action = "Contact" }
);

routes.MapRoute(
        name: "AboutUs",
        url: "about",
        defaults: new { controller = "Home", action = "About" }
);

routes.MapRoute(
        name: "CategoryDetails",
        url: "{seoName}",
        defaults: new { controller = "Category", action = "Details" }
);

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

With your original order, the urls~/contact and ~/about would always be handled by the seoName route. By reordering them, you make sure they are handled by the proper actions in the HomeController and the seoName route will only match a url after the contact and about routes have failed to match.

like image 63
Daniel J.G. Avatar answered Nov 15 '22 16:11

Daniel J.G.


The CategoryDetails route is defined first. That's why it matches for example the url "http://server/AboutUs" (seoName will be assigned "AboutUs"). The most specific routes (AboutUs, ContactUs) should be defined first.

like image 34
artokai Avatar answered Nov 15 '22 14:11

artokai


The following is causing the issue for you. please comment it out or place at the bottom.

routes.MapRoute(
     name: "CategoryDetails",
     url: "{seoName}",
     defaults: new { controller = "Category", action = "Details" }
);

Initally the Defualt route works and you are taken HomeControllers Indeax action.

When you click any Link. the routing engines matches the first one. the default values specified in the route definition are taken.

Try adding following line in controller and view . so that you will understand how and were you are going wrong

public ActionResult Details(string seoName)
    {
        ViewBag.ValueReceived = seoName;
        return View();
    } 

And in View

<h1>@ViewBag.ValueReceived</h1>

When Clicking Contact or Details the url will be resembling

http://arunkumar.com:62115/about

Here the 'about' is considered as value for seoName and the wrong routing is chosen

like image 30
Arunkumar Ravikumar Avatar answered Nov 15 '22 15:11

Arunkumar Ravikumar