仅在 IIS 部署后的 Asp.net Web api 异常:名为“HelpPage_Default”的路由已在路由集合中

Posted

技术标签:

【中文标题】仅在 IIS 部署后的 Asp.net Web api 异常:名为“HelpPage_Default”的路由已在路由集合中【英文标题】:Asp.net web api exception only after deploying at IIS : A route named 'HelpPage_Default' is already in the route collection 【发布时间】:2015-02-25 08:55:47 【问题描述】:

我已经阅读了this 问题并尝试了那里提到的解决方案,但只有在我将应用程序发布到远程服务器的 IIS 后,我才收到此异常。在我本地计算机的 IIS 中,应用程序运行良好。我不知道是什么导致该服务器出现此异常:

Server Error in '/' Application.

A route named 'HelpPage_Default' is already in the route collection. Route names must be unique.
Parameter name: name

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: A route named 'HelpPage_Default' is already in the route collection. Route names must be unique.
Parameter name: name
Source Error:  
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 

[ArgumentException: A route named 'HelpPage_Default' is already in the route collection. Route names must be unique.
Parameter name: name]
   System.Web.Routing.RouteCollection.Add(String name, RouteBase item) +3713577
   System.Web.Mvc.RouteCollectionExtensions.MapRoute(RouteCollection routes, String name, String url, Object defaults, Object constraints, String[] namespaces) +350
   System.Web.Mvc.AreaRegistrationContext.MapRoute(String name, String url, Object defaults, Object constraints, String[] namespaces) +95
   XbimServer.Areas.HelpPage.HelpPageAreaRegistration.RegisterArea(AreaRegistrationContext context) +174
   System.Web.Mvc.AreaRegistration.RegisterAllAreas(RouteCollection routes, IBuildManager buildManager, Object state) +323
   BimServer.WebApiApplication.Application_Start() +23

[HttpException (0x80004005): A route named 'HelpPage_Default' is already in the route collection. Route names must be unique.
Parameter name: name]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +12600317
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +175
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +304
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +404
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +475

[HttpException (0x80004005): A route named 'HelpPage_Default' is already in the route collection. Route names must be unique.
Parameter name: name]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +12617364
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +159
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +12456981

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34212

这是我在Global.asax.cs 设置的:

protected void Application_Start()
        
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        

我的RouteConfig.cs

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

还有我的WebApiConfig.cs

config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/controller/id",
        defaults: new  id = RouteParameter.Optional 
    );

我还对我的 web.config 文件进行了以下更改:

<system.webServer>
    <modules>
      <remove name="WebDAVModule" />
    </modules>
    <handlers>
      <remove name="WebDAV" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>

【问题讨论】:

你有没有想过这个问题?我也有同样的问题 【参考方案1】:

我刚刚遇到了同样的问题。我认为这个错误是因为我之前向我的网站推送了另一个解决方案,并且有剩余的文件以某种方式妨碍了我。

为了解决这个问题,我在通过 Visual Studio 发布到我的 Azure 站点时选中了“在目标位置删除其他文件”框。我假设您也可以在发布之前手动删除服务器上的任何旧文件。在此之后,网站运行良好,没有错误。

【讨论】:

我也是这样。我重命名了我的项目,因为我第一次弄错了命名空间,但是 .dll.pdb.config 文件仍保留在 bin 中文件夹。似乎 MVC 引擎选择了继承自 System.Web.HttpApplication 的所有内容并启动它,包括路由。 谢谢@HenkRoux!我试图做一个“干净的解决方案”,但在命名空间更改后文件夹保留了原始文件......必须手动从文件夹中删除它们...... 很好的解决方案,我的想法已经用完了,正在考虑回滚提交!如果有人在部署到 Azure 时遇到此问题,这应该可以解决(无需删除 bin 文件夹等)【参考方案2】:
    在 Visual Studio 中右键单击您的解决方案 -> 单击清理解决方案 删除 \Project\bin\ 文件夹下的所有文件 在 Visual Studio 中重建您的解决方案 干杯。

【讨论】:

我刚刚遇到了这个问题,因为我无意中在解决方案中添加了一个 Web 项目来引用另一个 Web 项目的引用。我确实运行了 clean,并删除了引用,但是,clean 没有从 bin 文件夹中删除 dll,所以我不得不手动删除它(列表中的第 2 步),这一切都开始工作了。【参考方案3】:

我遇到了同样的问题,并尝试了来自不同线程的多个提议的解决方案,但没有任何结果。

我终于从团队资源管理器中查看了一份干净的解决方案副本,并为我修复了它。

【讨论】:

【参考方案4】:

我通过重构重命名了一个命名空间,但这样做错过了这个硬编码的字符串:

Project > Areas > HelpPage > AppStart:

public static class HelpPageConfig
    
        [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters",
            MessageId = "<CORRECT NAMESPACE>.Areas.HelpPage.TextSample.#ctor(System.String)",

【讨论】:

是的 - 这在项目/命名空间重命名练习后为我修复了它【参考方案5】:

重命名项目名称后我遇到了同样的问题。所以,

    在整个解决方案中查找并替换正确的名称。 清洁解决方案 删除项目的 bin & obj 文件夹。 重建解决方案

..是的,它有效。

【讨论】:

我会忽略政策,因为你应该得到一个伟大的“谢谢”。【参考方案6】:

在本地 IIS 上部署应用程序时遇到了同样的问题。我的发布目录和 IIS 托管目录是相同的。 我尝试了以下步骤,它对我有用:

    点击解决方案>>发布 删除现有文件>> 设置>> 展开文件发布选项 选中复选框 Delete all existing files before publishing 为真; 选中复选框 Precompile during publishing 为 true。

【讨论】:

以上是关于仅在 IIS 部署后的 Asp.net Web api 异常:名为“HelpPage_Default”的路由已在路由集合中的主要内容,如果未能解决你的问题,请参考以下文章

请问IIS中如何部署asp.net项目?

将 ASP.NET Web API 部署到 IIS 服务器

iis支持asp.net4.0的注册命令使用方法及部署网站注意事项

将 asp.net core web api 部署到 IIS 7.5 时出现 500 错误

ASP.NET Web API 应用程序在 IIS 7 上部署时提供 404

ASP.NET Core Web 应用程序开发期间部署到IIS自定义主机域名并附加到进程调试