mvc 路由 - 如何将 /Products/ProductDetails/{id} 自定义为 /Products/{id}

mvc route - how to customize /Products/ProductDetails/{id} to /Products/{id}

我使用默认路由

默认url是http://example.com/Products/ProductsDetails/1

但我需要将其更改为 http://example.com/Products/1

请帮我解决这个问题。谢谢。

添加以下路线:

 routes.MapRoute(
                name: "Product",
                url: "Products/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

在 "Default" 路由之前添加上述内容意味着,如果您输入以 /Products/1 结尾的 url,您将被带到您想要的适当控制器和操作.

更改控制器和操作以满足您的需要。