[英]Routing multiple URLs to the same view in ASP.NET MVC
我在嘗試配置ASP.NET MVC項目以將多個URL路由到同一視圖時遇到一些麻煩。 給定以下URL:
localhost:1234
localhost:1234/Products
localhost:1234/Products/1
localhost:1234/Products/abcd
localhost:1234/Products/whatever
我希望它們中的每一個都能將用戶路由到同一視圖(例如Products.cshtml
)。
按照此站點上的示例,我已經用特殊的route屬性裝飾了Controller動作:
[HttpGet]
[Route("Products/{id?}")]
public ActionResult Products(string id)
{
return View();
}
在RouteConfig.cs
文件中,我設置了默認路由:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Products", action = "Products", id = UrlParameter.Optional }
);
localhost:1234
和localhost:1234/Products
鏈接都可以使用,但是不適用於其余的URL。
這些屬性對我來說看起來都是正確的,因此您可能只是忘了映射屬性路由。 請記住在初始化代碼中的某個地方調用MapMvcAttributeRoutes()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.