Search code examples
asp.net-coreweb-config

Suddenly caused problem : duplicate key error in asp.net core web.config at run time


When I run my nopCommerce project (asp.net core 3.1) web site, I see the error dialog that says there is duplicate keys in web.config file. but I see my web.config and there is not duplicate keys and sections.

I faced the same problem before and solved it but I cannot remember how. The problem is that in runtime, same web.config file loads for this application twice.

I know that I should remove a line from a configuration file in a runtime created folder but cannot remember where is it.


Solution

  • I found the soulution : When you run asp.net core web application in visual studio, it creates a hosting config file in [SolutionFolder]/.vs/[SolutionName]/config/applicationhost.config.

    In this file it specify existing websites in solution and their hosting information. for example :

    <sites>
      <site name="WebSite1" id="1" serverAutoStart="true">
        <application path="/">
          <virtualDirectory path="/" physicalPath="%IIS_SITES_HOME%\WebSite1" />
        </application>
        <bindings>
          <binding protocol="http" bindingInformation=":8080:localhost" />
        </bindings>
      </site>
      <site name="Nop.Web" id="4">
        <application path="/" applicationPool="Nop.Web AppPool">
          <virtualDirectory path="/" physicalPath="D:\Projects\AddressClick\Presentation\Nop.Web" />
        </application>
        <bindings>
          <binding protocol="http" bindingInformation="*:15537:localhost" />
        </bindings>
      </site>
      ....
      <applicationDefaults applicationPool="Clr4IntegratedAppPool" />
      <virtualDirectoryDefaults allowSubDirConfig="true" />
    </sites>
    

    My problem was that when I edited web project debug setting and changed it to run on IIS instead of IISExpress, it added a section to this file instead of editing existing section for it. So when I was running my web project, it tried to load two web.config files at runtime and faces Duplicate Key Error (my problem) when loading second section's configurations.

    I removed first Site section and problem solved.