我试着编写自定义路由。总是我有404错误有什么问题。我读了关于路由并且没弄明白。我想显示如下代码的内容,名称必须介于a-Z和0-9.Thanks。
之间 {action}/{name} /details/kll219dkl
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "home", action = "index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "test",
url: "details/{name}",
defaults: new { controller = "Test", action = "Details", name = "ujElk392ow" }
);
只是为了展示正在运作
public ActionResult Details(string name)
{
return Content(name);
}
答案 0 :(得分:0)
怎么样:
routes.MapRoute(
name: "test",
url: "{controller}/details/{name}",
defaults: new { controller = "Test", action = "Details", name = String.Empty },
constraints: new { name = @"^[a-zA-Z0-9]+$" }
);
哪会给你:
Url: Mapped Destination:
/Test/details/kll219dkl TextController->Details(name: "kll219dkl")
答案 1 :(得分:0)
除了布拉德回答。尝试更改路线的顺序。运行时采用适合请求的第一条路径。因此,路线必须从最肯定到最一般来定义。在您的订单中,运行时将始终采用默认路由。