简体   繁体   English

ASP.net核心路线/ prod / 1 / review / 1

[英]ASP.net core Routes /prod/1/review/1

I read a lot of blogs and questions available regarding the routes in asp.net core but did not find any one mentioning something similar. 我阅读了很多关于asp.net核心路线的博客和问题,但没有找到任何类似的东西。

In some conditions I want my routes to look like: 在某些情况下,我希望我的路线看起来像:

/products/1/reviews/1

Where products and reviews are followed by their id OR more nested route like: 产品和评论后面跟着他们的id或更多的嵌套路线,如:

/products/1/images/1/comments/1

Is there a way I can define a template route for it like the default one: 有没有一种方法可以为它定义模板路由,就像默认路由一样:

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

You can use Attribute based routing 您可以使用基于属性的路由

[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 - Routing in ASP.NET Core https://docs.asp.net/en/latest/fundamentals/routing.html - ASP.NET Core中的路由

There is no difference with the route format of ASP.NET and ASP.NET Core. ASP.NET和ASP.NET Core的路由格式没有区别。

You can check this one: ASP.NET Routing 您可以检查以下内容: ASP.NET路由

To achieve your goal, all you have to do is define the route ON TOP of your default route. 为了实现您的目标,您所要做的就是定义默认路线的ON TOP路线。

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

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