【发布时间】:2015-12-26 04:56:17
【问题描述】:
我正在尝试向我的 Asp.net MVC 网站添加正确的路由。
- www.example.com/profile/39 ( Profile/{id} ):用于显示某人的个人资料。
- www.example.com/profile/edit (Profile/edit):用于编辑当前用户配置文件
这是我的路线:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("ShowProfile",
"Profile/{id}",
new { controller = "Profile", action = "Index" });
routes.MapRoute(
name:"Profile",
url: "{controller}/{action}"
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
我的问题是,当我将 ShowProfile 路由放在首位时,它会正确显示配置文件,但是当我输入 www.example.com/profile/edit 时,它会显示缺少 Int 值的错误,而当将 Profile 路由放在首位时,会为 @ 显示 404 错误987654326@.
我试图通过改变我的路线来解决这个问题:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("EditProfile",
"Profile/Edit",
new { controller = "Profile", action = "Edit" });
routes.MapRoute("ShowProfile",
"Profile/{id}",
new { controller = "Profile", action = "Index" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
在这种格式下,这两种情况都可以正常工作,但对于其他发布方法,它会显示 404 错误,例如:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditEmail()
{
return RedirectToAction("Index","Home");
}
但我不想为我的所有方法添加路由值,有什么通用的方法可以不添加一个路由值吗?
【问题讨论】:
标签: jquery asp.net ajax asp.net-mvc routes