ASP.NET MVC routing not working

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

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 us about us or contact us, he leads me to a method of action with details in the category controller.

This is the markup for me about us and contact us:

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

Powerful action method for category controller:

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

     return View(model);
}

What is wrong with my route configuration?

+4
source share
4 answers

Change the order of the routes from the most specific to the less specific. Thus, the routes for the contact and so on appear before the route seoName:

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 }
);

URL- ~/contact ~/about seoName. , , HomeController, seoName URL- , .

+26

CategoryDetails. , , URL- "http://server/AboutUs" (seoName "AboutUs" ). (AboutUs, ContactUs).

+3

. .

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

Defualt, MainControllers Indeax.

. . , , .

. , .

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

<h1>@ViewBag.ValueReceived</h1>

When you click a contact or Details, the URL will resemble

http://arunkumar.com:62115/about

Here "about" is considered the value for seoName and the wrong routing is selected

+1
source

I had a similar error, I put

HttpPost ("/ route")
instead
HttpPost ("route")
0
source

Source: https://habr.com/ru/post/1523870/


All Articles