0
我想添加一个正确的路由到我的Asp.net MVC网站。ASP.NET MVC URL路由与单路径地图
- www.example.com/profile/39(资料/ {ID}):其用于示出一些一项的轮廓。
- www.example.com/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
路线放入www.example.com/profile/39
时显示404错误。
我试图用改变我的途径来解决这个问题:这种格式
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");
}
但我不想为我的所有方法添加路由值,有没有一种通用的方式来不添加逐个路由值?