Asp.net-Mvc

僅在 IIS 上部署後的 Asp.net Web api 異常:名為“HelpPage_Default”的路由已在路由集合中

  • December 29, 2014

我已經閱讀了這個問題並嘗試了那裡提到的解決方案,但是只有在我將應用程序發佈到遠端伺服器的 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>

我剛剛遇到了同樣的問題。我認為這個錯誤是因為我之前向我的網站推送了另一個解決方案,並且有剩餘的文件以某種方式妨礙了我。

為了解決這個問題,我在通過 Visual Studio 發佈到我的 Azure 站點時選中了“在目標位置刪除其他文件”框。我假設您也可以在發布之前手動刪除伺服器上的任何舊文件。在此之後,該網站執行良好,沒有錯誤。

引用自:https://stackoverflow.com/questions/27684341