简体   繁体   English

ASP.NET MVC路由无法正常工作

[英]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? 我的路线配置有什么问题?

Reorder your routes from most specific to less specific. 将您的路线从最具体到特定的重新排序。 That way the routes for contact and about will come before the seoName 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 }
);

With your original order, the urls ~/contact and ~/about would always be handled by the seoName route. 使用您的原始订单,网址~/contact~/about将始终由seoName路由处理。 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. 通过重新排序它们,您可以确保它们由HomeController的正确操作处理,并且seoName路由将仅在联系之后匹配URL并且路由无法匹配。

The CategoryDetails route is defined first. 首先定义CategoryDe​​tails路由。 That's why it matches for example the url " http://server/AboutUs " (seoName will be assigned "AboutUs"). 这就是它匹配的原因,例如url“ http://server/AboutUs AboutUs”(seoName将被分配为“AboutUs”)。 The most specific routes (AboutUs, ContactUs) should be defined first. 应首先定义最具体的路由(AboutUs,ContactUs)。

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. 最初Defualt路线工作,你采取HomeControllers Indeax行动。

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 http://arunkumar.com:62115/about

Here the 'about' is considered as value for seoName and the wrong routing is chosen 这里'about'被认为是seoName的值,选择了错误的路由

I was having a similar error, i was putting 我有一个类似的错误,我正在推杆

HttpPost("/route")
instead of 代替

 HttpPost("route") HttpPost( “路径”) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM