“/”应用程序中的服务器错误。


找到多个与名为“Home”的控制器匹配的类型。如果为此请求(“{controller}/{action}/{id}”)提供服务的路由没有指定命名空间以搜索与此请求相匹配的控制器,则会发生这种情况。如果是这样,请通过调用带有 'namespaces' 参数的 "MapRoute" 方法的重载来注册此路由。

“Home”请求找到下列匹配的控制器:
WebAppAreasDemo.Controllers.HomeController
WebAppAreasDemo.Areas.PharmaceuticalCompanies.Controllers.HomeController

说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidOperationException: 找到多个与名为“Home”的控制器匹配的类型。如果为此请求(“{controller}/{action}/{id}”)提供服务的路由没有指定命名空间以搜索与此请求相匹配的控制器,则会发生这种情况。如果是这样,请通过调用带有 'namespaces' 参数的 "MapRoute" 方法的重载来注册此路由。

“Home”请求找到下列匹配的控制器:
WebAppAreasDemo.Controllers.HomeController
WebAppAreasDemo.Areas.PharmaceuticalCompanies.Controllers.HomeController

源错误:


执行当前 Web 请求期间生成了未经处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。

堆栈跟踪:

[InvalidOperationException: 找到多个与名为“Home”的控制器匹配的类型。如果为此请求(“{controller}/{action}/{id}”)提供服务的路由没有指定命名空间以搜索与此请求相匹配的控制器,则会发生这种情况。如果是这样,请通过调用带有 'namespaces' 参数的 "MapRoute" 方法的重载来注册此路由。

“Home”请求找到下列匹配的控制器:
WebAppAreasDemo.Controllers.HomeController
WebAppAreasDemo.Areas.PharmaceuticalCompanies.Controllers.HomeController]
System.Web.Mvc.DefaultControllerFactory.GetControllerTypeWithinNamespaces(RouteBase route, String controllerName, HashSet`1 namespaces) +159
System.Web.Mvc.DefaultControllerFactory.GetControllerType(RequestContext requestContext, String controllerName) +544
System.Web.Mvc.DefaultControllerFactory.System.Web.Mvc.IControllerFactory.GetControllerSessionBehavior(RequestContext requestContext, String controllerName) +53
System.Web.Mvc.MvcRouteHandler.GetSessionStateBehavior(RequestContext requestContext) +132
System.Web.Mvc.MvcRouteHandler.GetHttpHandler(RequestContext requestContext) +33
System.Web.Mvc.MvcRouteHandler.System.Web.Routing.IRouteHandler.GetHttpHandler(RequestContext requestContext) +10
System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context) +9843503
System.Web.Routing.UrlRoutingModule.OnApplicationPostResolveRequestCache(Object sender, EventArgs e) +82
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +141
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69

版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.6.1055.0

解决方法:


RouteConfig.cs注册路由添加命名空间(namespaces)参数

namespace WebAppAreasDemo
{
public class RouteConfig
{
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 },
namespaces: new string[] { "WebAppAreasDemo.Controllers" }
);
}
}
}

现在访问http://localhost:2353/正常了,然而只输入区域名称访问http://localhost:2353/PharmaceuticalCompanies/,提示如下:

“/”应用程序中的服务器错误。


无法找到资源。

说明: HTTP 404。您正在查找的资源(或者它的一个依赖项)可能已被移除,或其名称已更改,或暂时不可用。请检查以下 URL 并确保其拼写正确。

请求的 URL: /PharmaceuticalCompanies/


版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.6.1055.0


这又是闹什么鬼,看下区域下的PharmaceuticalCompaniesAreaRegistration.cs注册类,发现没有设置默认的控制器

namespace WebAppAreasDemo.Areas.PharmaceuticalCompanies
{
public class PharmaceuticalCompaniesAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "PharmaceuticalCompanies";
}
} public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"PharmaceuticalCompanies_default",
"PharmaceuticalCompanies/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
}

修改

new { action = "Index", id = UrlParameter.Optional }

添加默认的控制器名称

new { controller="Home", action = "Index", id = UrlParameter.Optional }

现在再只输入区域名称访问http://localhost:2353/PharmaceuticalCompanies/,终于正常了。

最新文章

  1. ASP.NET Core HTTP 管道中的那些事儿
  2. Linux 下.desktop 桌面程序图标文件编写方式
  3. Jpeg2000 简介
  4. Myeclipse+maven时Tomcat部署时maven的依赖文件不能部署到Tomcat上
  5. 导出resource文件的的资源
  6. CMake with Win&MinGW
  7. Builder 模式
  8. utf-8的mysql表笔记
  9. A Simple Problem with Integers(线段树,区间更新)
  10. 寒假学干货之------ 初学者关于fragment_main(碎片的困扰)
  11. python基础(二)字符串內建函数详解
  12. 短信发送接口被恶意访问的网络攻击事件(四)完结篇--搭建WAF清理战场
  13. Java Servlet 笔记4
  14. ubuntu制作离线包
  15. AI_群组行为
  16. Overrid Equals Defined Operator
  17. 【Java】字节数组转换工具类
  18. Windows下memcache的配置和使用(python)
  19. Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test)
  20. maven 插件jetty/tomcat启动 web 应用

热门文章

  1. VS2019 实用设置
  2. 01构建第一个SpringBoot工程
  3. 第二部分之Redis服务器(第十四章)
  4. JS中让新手倍感震惊、违反直觉、出乎意料、的一些知识点汇总记录
  5. 动态chart Demo
  6. Linux几大服务
  7. Linux学习之路1
  8. 为知笔记Linux版编译使用记录
  9. BZOJ 2730 矿场搭建
  10. java day02 记录