asp.net-mvc 仅在 IIS 上部署后,Asp.net web api 异常:名为“HelpPage_Default”的路由已在路由集合中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27684341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:21:06  来源:igfitidea点击:

Asp.net web api exception only after deploying at IIS : A route named 'HelpPage_Default' is already in the route collection

asp.net-mvciisasp.net-web-api

提问by Aminul

I've read thisquestion and tried the solutions mentioned there but I'm getting this exception only after i published the application to IIS of a remote server. In my local computer's IIS the application is working fine. I've no clue what is causing this exception at that server:

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

Here's what I've set up at Global.asax.cs:

这是我设置的内容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);
        }

My RouteConfig.cs:

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

And my WebApiConfig.cs:

而我的WebApiConfig.cs

config.MapHttpAttributeRoutes();

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

I've also made the following change to my web.config file:

我还对我的 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>

回答by Tibincrunch

I just ran into the same issue. I think the error was caused because I had pushed another solution to my site previously and there were leftover files that were somehow getting in the way.

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

To fix this I checked the box that says "Remove additional files at destination" while publishing through Visual Studio to my Azure site. I would assume you could just manually delete any old files that were on the server before publishing as well. After this the site ran fine with no errors.

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

回答by Aminul

  1. Right click your solution in Visual Studio -> click Clean Solution
  2. Remove all files under your \Project\bin\ folders
  3. Rebuild your solution in Visual Studio Cheers.
  1. 在 Visual Studio 中右键单击您的解决方案 -> 单击 Clean Solution
  2. 删除 \Project\bin\ 文件夹下的所有文件
  3. 在 Visual Studio Cheers 中重建您的解决方案。

回答by Jeremy Thompson

I renamed a Namespace by refactoring it and doing that missed this hardcoded string:

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

Project > Areas > HelpPage > AppStart:

项目 > 区域 > 帮助页面 > 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)",

回答by King_Fisher

I have the same issue after renamed the project name. So,

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

  1. Find & Replace the correct name across the entire solution.
  2. Clean solution
  3. Delete bin & obj folder of the project.
  4. Rebuild solution
  1. 在整个解决方案中查找并替换正确的名称。
  2. 清洁液
  3. 删除项目的 bin & obj 文件夹。
  4. 重建解决方案

..yes, it works.

..是的,它有效。

回答by aemorales1

I had this same issue and tried multiple proposed solutions from different threads with similar issues and got nowhere.

我遇到了同样的问题,并尝试了来自具有类似问题的不同线程的多个建议解决方案,但一无所获。

I finally just checked out a clean copy of the solution from team explorer and that fixed it for me.

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

回答by Santosh P

I had the same issue when deploying application on Local IIS. My publish directory and IIS hosting directory are same. I tried with following steps, it worked for me:

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

  1. Click on Solution >> publish
  2. Delete existing file>> settings>> expand file publish options
  3. check checkbox Delete all existing files prior to publish is true;
  4. check checkbox Precompile during publishing is true.
  1. 点击解决方案>>发布
  2. 删除现有文件>>设置>>展开文件发布选项
  3. checkbox 在发布之前删除所有现有文件为真;
  4. checkbox 发布期间的预编译为真。