gpt4 book ai didi

asp.net-mvc - ASP.NET MVC 中的区域路由

转载 作者:行者123 更新时间:2023-12-05 01:16:11 24 4
gpt4 key购买 nike

我对区域路由有点困惑。我创建了一个名为 Backbone 的区域。我也有我的默认 Controller 、 View 和模型。

http://localhost:46870/ 

给我以下错误:

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

这里是 Backbone 路线(这是脚手架自带的,我没有做任何改动):

    public override void RegisterArea(AreaRegistrationContext context) 
{
context.MapRoute(
"BackBone_default",
"BackBone/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}

默认路线(这是脚手架自带的,我没有做任何改变):

    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 }
);
}

我在想除非url以

开头
http://localhost:46870/Backbone

Backbone 区家不会被调用。那么,为什么路由对此感到困惑。

最令人困惑的部分是当我调用这个 url 时:

http://localhost:46870/home/index

它向我显示相同的错误消息。为什么 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"}

        context.MapRoute(
"BackBone_default",
"BackBone/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "Test.Areas.Backbone.Controllers" }

关于asp.net-mvc - ASP.NET MVC 中的区域路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25022712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com