[英]How can I configure ASP.NET MVC routing in C#?
有一個關於 ASP.NET MVC 中路由的問題。
我想在我的應用程序中有很好的 URL,這就是我使用路由的原因
context.MapRoute(
name: "ManageProducts_Home",
url: "Eshop/ManageProducts/{eshopId}",
defaults: new { controller = "ManageProducts", action = "Home", AreaName = "Eshop" }
);
這給了我很好的網址,比如
https://localhost:44381/Eshop/ManageProducts/22
這太棒了!
然后我有另一條路線:
context.MapRoute(
name: "ManageProducts_AddProduct",
url: "Eshop/ManageProducts/AddProduct/{eshopId}/{topCategoryId}",
defaults: new { controller = "ManageProducts", action = "AddProduct", AreaName = "Eshop" }
);
產生這個 URL:
https://localhost:44381/Eshop/ManageProducts/AddProduct/22/1
好的,到目前為止一切順利。
但是在第二頁( AddProduct
)上,我需要通過我這樣稱呼的 AJAX 刷新一些部分視圖
$("._SelectCategory").load("/Eshop/ManageProducts/_SelectCategory", { categoryId: categoryId });
controller 中當然有一個動作_SelectCategory
,它帶有一個參數categoryId
。
問題是,這個調用被第一個路由捕獲並且失敗,異常缺少eshopId
in action Home,這意味着這個調用不會 go 到我那里的默認路由
context.MapRoute(
"Eshop_default",
"Eshop/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
我需要一些建議如何在 AddProduct 視圖中配置路由AddProduct
調用。
謝謝
使用 Web API 空 Controller 並提供更高效的路線,例如:在 ManageProducts Z5903C1AZ53F2FC03E04
[HttpGet]
[Route("api/Eshop/ManageProducts/{eshopId}")]
在您的 Startup.cs 配置中添加以下內容: app.UseMvc();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.