【问题标题】:An attempt to override an existing mapping was detected for type System.Web.IHttpHandler检测到 System.Web.IHttpHandler 类型尝试覆盖现有映射
【发布时间】:2026-02-01 02:05:01
【问题描述】:

当我将 asp.net mvc 应用程序复制到我们的测试服务器的 IIS 文件夹时,出现以下错误。

在本地它工作得很好:

检测到名称为“”的类型 System.Web.IHttpHandler 覆盖现有映射的尝试,当前映射到类型 Microsoft.Reporting.WebForms.HttpHandler,以键入 Microsoft.Reporting.WebForms.HttpHandler。

UnityConfig.cs 代码如下:

namespace xxx.Relacionamiento.Web.App_Start
{
    /// <summary>
    /// Specifies the Unity configuration for the main container.
    /// </summary>
    public class UnityConfig
    {
        #region Unity Container
        private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
        {
            var container = new UnityContainer();
            RegisterTypes(container);
            return container;
        });

        /// <summary>
        /// Gets the configured Unity container.
        /// </summary>
        public static IUnityContainer GetConfiguredContainer()
        {
            return container.Value;
        }
        #endregion

        /// <summary>Registers the type mappings with the Unity container.</summary>
        /// <param name="container">The unity container to configure.</param>
        /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to 
        /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
        public static void RegisterTypes(IUnityContainer container)
        {
            // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
            container.RegisterType<IUnitOfWork, EntityFrameworkUnitOfWork>(new PerRequestLifetimeManager());
            container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager());
            container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
            container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
            container.RegisterType<AccountController>(new InjectionConstructor(
                //new ResolvedParameter<ApplicationUserManager>("userManager"),
                //new ResolvedParameter<ApplicationSignInManager>("signInManager"),
                new ResolvedParameter<DatosExternosService>("DatosExternos"),
                new ResolvedParameter<UserRolesServices>("UserRolesServices"),
                new ResolvedParameter<AspNetUserService>("AspNetUserService"),
                new ResolvedParameter<AsesoresService>("AsesoreService"),
                new ResolvedParameter<AsociadosService>("AsociadoService")
                ));
            container.RegisterTypes(
                AllClasses.FromLoadedAssemblies(), WithMappings.FromMatchingInterface, WithName.Default
               );
        }
    }
}

web.config

<system.web>
    <!--<globalization uiCulture="es" culture="ES" />-->
    <globalization uiCulture="auto" culture="auto" />
    <!--<httpRuntime maxRequestLength="1048576" />-->
    <httpHandlers>
      <add path="Reserved.ReportVidewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false" />
      <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false" />
      <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" validate="false" />
   </httpHandlers>
    <customErrors mode="Off"></customErrors>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5.1">
      <assemblies>
        <add assembly="Microsoft.ReportViewer.WebForms, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
        <add assembly="Microsoft.ReportViewer.Common, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
        <add assembly="Microsoft.Build.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
      </assemblies>

我已经用谷歌搜索了错误,但没有给出 results。

【问题讨论】:

    标签: c# asp.net asp.net-mvc reporting-services unity-container


    【解决方案1】:

    由于RegisterTypes 方法抛出DuplicateTypeMappingException,您可能已经有一个注册映射,该映射也与RegisterTypes 上定义的约定之一相匹配——称为“按约定注册”。

    RegisterTypes 方法返回与RegistrationConvention 相关的IUnityContainer,如下所示:

    public static IUnityContainer RegisterTypes(
        this IUnityContainer container,
        RegistrationConvention convention,
        bool overwriteExistingMappings = false)
    

    然后,这解释了可选的overwriteExistingMappings 参数的工作原理:

    使用此参数来控制RegisterTypes 方法的行为 如果此方法的参数导致在 要覆盖的容器。现有的映射可能是 由先前调用创建一个或多个类型或在 当前对 RegisterTypes 方法的调用。如果设置了这个参数 为 false,该方法将引发 DuplicateTypeMappingException 异常 如果它检测到覆盖现有映射的尝试。如果 参数设置为 true,该方法将覆盖现有映射 使用基于方法的其他参数的新映射。这 此参数的默认值为 false

    由于在 web.config 中设置了预定义的 httpHandlers 并且默认情况下 overwriteExistingMappings 设置为 false,因此它会检测到尝试覆盖现有的 httpHandlers 映射并按预期抛出 DuplicateTypeMappingException

    根据上面的解释,您可以尝试在此代码部分中将overwriteExistingMappings 参数集添加为 true

    container.RegisterTypes(AllClasses.FromLoadedAssemblies(), WithMappings.FromMatchingInterface, WithName.Default);
    

    到这个:

    container.RegisterTypes(AllClasses.FromLoadedAssemblies(), WithMappings.FromMatchingInterface, WithName.Default, overwriteExistingMappings: true);
    

    注意:这只防止RegisterTypes 在尝试覆盖现有映射时抛出异常。如果你不确定Microsoft.Reporting.WebForms.HttpHandler的配置没有被覆盖,可以使用DI方式在RegisterTypes方法中注册ReportViewer映射。

    参考:

    Registration by Convention (MSDN)

    其他参考资料:

    Unity Container: Dependency Injection with Unity (MSDN)

    Convention based registrations with Unity

    【讨论】:

      【解决方案2】:

      您可能需要清除所有继承的 HttpHandlers

      <httpHandlers>
        <clear/>
        <add path="...
      

      【讨论】: