仅在IIS部署后才会出现Asp.net web api异常:路径集合中已有一条名为"HelpPage_Default"的路由

Ami*_*nul 33 iis asp.net-mvc asp.net-web-api

我已经阅读了这个问题,并尝试了那里提到的解决方案,但是只有在我将应用程序发布到远程服务器的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
Run Code Online (Sandbox Code Playgroud)

以下是我设置的内容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);
        }
Run Code Online (Sandbox Code Playgroud)

我的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 }
            );
        }
Run Code Online (Sandbox Code Playgroud)

我的WebApiConfig.cs:

config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
Run Code Online (Sandbox Code Playgroud)

我还对我的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>
Run Code Online (Sandbox Code Playgroud)

小智 68

我刚遇到同样的问题.我认为错误是因为我之前已经将另一个解决方案推送到我的网站,并且有一些剩余的文件在某种程度上妨碍了我.

为了解决这个问题,我在通过Visual Studio发布到我的Azure站点时选中了"在目的地删除其他文件"框.我想你可以在发布之前手动删除服务器上的任何旧文件.在此之后,该网站运行良好,没有错误.

  • 这也是我的情况.我重命名了我的项目,因为我第一次得到了命名空间错误,但**.dll**,**.pdb**和**.config**文件仍保留在bin文件夹中.看起来MVC引擎会从System.Web.HttpApplication中继承所有内容并启动它,包括路由. (24认同)

小智 21

  1. 右键单击Visual Studio中的解决方案 - >单击"清理解决方案"
  2. 删除\ Project\bin \文件夹下的所有文件
  3. 在Visual Studio Cheers中重建您的解决方案.


Jer*_*son 8

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

项目 > 区域 > 帮助页面 > 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)",
Run Code Online (Sandbox Code Playgroud)