[英]ASP.net core Routes /prod/1/review/1
我閱讀了很多關於asp.net核心路線的博客和問題,但沒有找到任何類似的東西。
在某些情況下,我希望我的路線看起來像:
/products/1/reviews/1
產品和評論后面跟着他們的id或更多的嵌套路線,如:
/products/1/images/1/comments/1
有沒有一種方法可以為它定義模板路由,就像默認路由一樣:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
您可以使用基於屬性的路由
[Route("[conroller]/[action]")]
public class ImagesController
{
[HttpGet("/[controller]/{productId:int}/images/{imageId:int}/comments/{commentId:int}")]
public IActionResult GetComments(int productId, int imageId, int commentId)
{
}
}
routes.MapRoute(
name: "Products",
template: "products/{productId}/{action?}/{commentId?}",
defaults: new { controller = "Products", action = "Index" });
https://docs.asp.net/en/latest/fundamentals/routing.html - ASP.NET Core中的路由
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.