我有一个简单的 MVC4 网站,其中包含一个区域(称为“用户”),其中包含一个名为“HomeController”的控制器。
在这个控制器上有两个动作方法:Index 和 Details:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Details(int id)
{
return View();
}
}
“UserAreaRegistration.cs”类如下:
public class UserAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "User";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"User_default",
"User/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MvcApplication1.Areas.User.Controllers" }
);
context.MapRoute(
"User_default_no_contoller",
"User/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MvcApplication1.Areas.User.Controllers" }
);
}
}
还有一个不在名为“HomeController”的区域中的控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
RouteConfig.cs(针对非区域路由)如下:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MvcApplication1.Controllers" }
);
}
}
我可以通过任一方式访问“主页”
- /
- /家
- /首页/索引
我还可以在“用户”区域中访问“索引”操作
- /用户
我不能做的是弄清楚如何在名为“详细信息”的区域中使用我的控制器上的另一种方法:
- /User/Details/5 不起作用,它返回 404
我还注意到,尝试显式访问 Index 方法也没有正确路由:
- /User/Index 不起作用,它返回 404
我究竟做错了什么?
是否可以在作为区域中“默认”控制器的控制器上使用“索引”以外的任何操作方法?