sam*_*113 3 asp.net-mvc asp.net-routing
我对区域路由有点困惑.我创建了一个名为Backbone的区域.我有我的默认控制器,视图和模型.
http://localhost:46870/
Run Code Online (Sandbox Code Playgroud)
给我以下错误:
Multiple types were found that match the controller named 'home'. This can happen if
the route that services this request ('{controller}/{action}/{id}') does not specify
namespaces to search for a controller that matches the request. If this is the case,
register this route by calling an overload of the 'MapRoute' method that takes a
'namespaces' parameter.
The request for 'home' has found the following matching controllers:
LearnJavascript.Areas.BackBone.Controllers.HomeController
LearnJavascript.Controllers.HomeController
Run Code Online (Sandbox Code Playgroud)
这是骨干路线(这带有脚手架,我没有做任何改动):
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"BackBone_default",
"BackBone/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
Run Code Online (Sandbox Code Playgroud)
默认路由(这是脚手架,我没有做任何更改):
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Run Code Online (Sandbox Code Playgroud)
我在想除非网址开头
http://localhost:46870/Backbone
Run Code Online (Sandbox Code Playgroud)
家里的骨干区域不会被召唤.那么,为什么路由会对此感到困惑.
最令人困惑的部分是我打电话给这个网址:
http://localhost:46870/home/index
Run Code Online (Sandbox Code Playgroud)
它向我显示了相同的错误消息.为什么MVC路由在这方面如此混乱.
我使用VS2013和MVC5.
我从HamidBahmanabady那里得到了正确答案.
我将命名空间添加到全局路由,它开始工作正常.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Test.Controllers"}
Run Code Online (Sandbox Code Playgroud)
和
context.MapRoute(
"BackBone_default",
"BackBone/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "Test.Areas.Backbone.Controllers" }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5306 次 |
| 最近记录: |