Asp.net mvc 4 子文件夹ASP.Net MVC 4中的控制器
在我的ASP.NETMVC4项目中,控制器文件夹中的子文件夹中有一个控制器-Asp.net mvc 4 子文件夹ASP.Net MVC 4中的控制器,asp.net-mvc-4,Asp.net Mvc 4,在我的ASP.NETMVC4项目中,控制器文件夹中的子文件夹中有一个控制器- /Controllers /GroupA /AbcController.cs 在AbcController中,我有两种方法- public ActionResult Index() { return View(); } public ActionResult Edit(string value) { ViewBag.Me
/Controllers
/GroupA
/AbcController.cs
在AbcController中,我有两种方法-
public ActionResult Index()
{
return View();
}
public ActionResult Edit(string value)
{
ViewBag.Message = value;
return View();
}
RouteConfig.cs-
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "TestRoute",
url: "GroupA/{controller}/{action}/{id}",
defaults: new { controller = "AbcController", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
但是当我浏览http://localhost:2240/groupa/abc/edit/somevalue
,未将“somevalue”传递给该方法。它显示null
这里我遗漏了什么?在您的路径中,您的参数被声明为
id
,而在您的操作方法中,它被声明为value
。挑一个,坚持到底
routes.MapRoute(
name: "TestRoute",
url: "GroupA/{controller}/{action}/{value}",
defaults: new { controller = "AbcController", action = "Index", value = UrlParameter.Optional }
);
public ActionResult Edit(string value)
{
ViewBag.Message = value;
return View();
}
编辑:当我们谈到这个话题时,我建议你看一看。如果我没记错的话,是不是可以通过
../groupa/abc/Edit?somevalue
作为url或将public ActionResult Edit(string value)
更改为public ActionResult Edit(string id)
来解决这个问题,也就是说,你在路由中命名你的参数id
,但是该方法中的value
将为您的路由(“子文件夹”)提供另一个层次结构级别。@Jasen,是的,MVCArea
是一个不错的选择。在此之前,我也使用了OP的工作方式,但直到最近我才了解了区域
,而且它非常容易使用。