gpt4 book ai didi

c# - ASP.NET MVC 路由不工作

转载 作者:太空狗 更新时间:2023-10-29 18:01:21 24 4
gpt4 key购买 nike

我的路由工作不正常。我定义了以下路线:

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

当我点击关于我们或联系我们的链接时,它会将我带到类别 Controller 中的详细操作方法。

这是我的关于我们和联系我们链接的标记:

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

我的类别 Controller 的详细操作方法:

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

return View(model);
}

我的路由配置有什么问题?

最佳答案

将您的路线从最具体到不太具体重新排序。这样 contact 和 about 的路由就会出现在 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 }
);

对于您的原始订单,urls~/contact~/about 将始终由 seoName 路由处理。通过对它们重新排序,您可以确保它们由 HomeController 中的正确操作处理,并且 seoName 路由将仅在联系人和 about 路由匹配失败后匹配 url。

关于c# - ASP.NET MVC 路由不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21377807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com