C# 如何在URL中没有操作的情况下获取路由
我希望我的路线看起来像:C# 如何在URL中没有操作的情况下获取路由,c#,asp.net-mvc,C#,Asp.net Mvc,我希望我的路线看起来像: /product/123 我有一个动作GET,但我不想在URL中出现,它当前是: /product/get/123 如何得到这个 Global.asax.cs RouteConfig.RegisterRoutes(RouteTable.Routes); public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathIn
/product/123
我有一个动作GET,但我不想在URL中出现,它当前是:
/product/get/123
如何得到这个
Global.asax.cs
RouteConfig.RegisterRoutes(RouteTable.Routes);
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "MyApp.Web.Controllers" }
);
}
可以使用“路线”属性定义路径,如下所示:
[Route("product")]
public class ProductController {
[Route("{productId}"]
public ActionResult Get(int productId) {
// your code here
}
}
它为您提供了“/product/{productId}”的完整路由定义,在您的案例中是“/product/123”。更多详细信息:在routes.MapRoute中将此代码添加到默认路由之前
routes.MapRoute(
name: "Product",
url: "Product/{id}",
defaults: new { controller = "product", action = "get"}
告诉我们您是如何设置路由的。@艾米,我添加了我的路由配置。如何使用新的3.0路由工具来实现这一点?