51

In Visual studio, Solution->Web.Project->Properties->Web, I have changed my Project Url from http://localhost:51123/ to http://localhost:51123/NewProjectName and I keep getting this error:

"Cannot add duplicate collection entry of type 'add' with unique key attribute 'name' set to ...." on every module added.

Adding a remove tag works but then it should have been a problem even before i changed the url. Any suggestions?

1
  • Hi Prasith, may I ask which solution worked for you please?
    – sanepete
    Commented Jan 7, 2016 at 12:21

14 Answers 14

71

I think IIS Express probably has 2 <application>-blocks and both will be pointing to the same physicalPath.

Go to the IIS Express config file in: My Documents\IISExpress\config\applicationhost.config

Search for NewProjectName

Change the physicalPath for the root application to something else. Point it to an empty folder.

Should look something like this:

<site name="NewProjectName" id="1">
  <application path="/" applicationPool="Clr4IntegratedAppPool">
    <virtualDirectory path="/" physicalPath="c:\Temp" />
  </application>
  <application path="/NewProjectName" applicationPool="Clr4IntegratedAppPool">
    <virtualDirectory path="/" physicalPath="c:\sourcecode\NewProjectName" />
  </application>
  <bindings>
    <binding protocol="http" bindingInformation="*:51123:localhost" />
  </bindings>
</site>
6
  • 6
    For IIS, the config file is @ C:\Windows\System32\inetsrv\config Commented Sep 18, 2014 at 12:05
  • 9
    In Visual Studio 2015 the location of the cofig file is solutionFolder\.vs\cofig. Note that .vs is hidden. Commented Feb 28, 2017 at 19:39
  • 2
    Same with VS 2017 - the config file is under [Solution Folder Root]\.vs\config\applicationhost.config.
    – dotNetkow
    Commented Nov 2, 2017 at 16:01
  • I had the same problem with IIS, and the root cause was the same. Two nested applications were hosted, both of which pointed to the same site configuration. The issue was resolved after I deleted the child app. Commented Apr 30, 2021 at 17:48
  • for IIS10 C:\Windows\System32\inetsrv\config\applicationHost.config
    – MeSo2
    Commented Dec 31, 2022 at 14:54
26

Also, ensure you don't have a duplicate web.config file in one of the parent folders (eg: a web.config backup file). That was the issue with mine!

2
  • 1
    Good tip! I had an automated web deployment go awry and deploy the web.config up one directory. Removing that file resolved this issue for me. Commented Aug 27, 2015 at 19:00
  • Keep in mind that a "parent" folder could be a parent web folder. For example, in my case, the site was an application inside a website. The physical path was a completely separate folder structure than the site, but the parent folder was the physical path of the site, not the physical folder parent. Hope that makes sense. Commented Sep 30, 2020 at 12:55
19

I started randomly getting this error. I noticed that the iis express had two sites.

Screenshot of two websites running in IIS express

This double layer is causing IIS to read the web.config from the first site and second at \WFM, therefore finding duplicates. I just stopped all the sites and removed the \WFM from my web project path. However you could go clear the files and folders from your temp file in IIS express. In my case I had multiple versions of the solution and one solution's project\user config had an extra path in the web project URL. VS 2015 added it, or someone checked in their own user config to TFS. Hope this helps others.

3
  • 6
    In case someone is looking for this: I had to remove the .vs-Folder in solution path and the <project-name>.user-file.
    – MHolzmayr
    Commented Apr 12, 2016 at 4:20
  • Mine looked like this as well, but clearing temporary files didn't work for me. I had to remove my site's entire <application/> node from my IIS Express's applicationhost.config, found under ...\My Documents\IISExpress\config
    – Danny
    Commented Jul 18, 2016 at 17:03
  • I inadvertently had created a new IIS Express virtual directory for the same app. I found a duplicate entry in the .vs folder, inside applicationhost.config. Once I removed the duplicate entry everything worked again. Commented Feb 10, 2017 at 15:20
17

All web.config files work off multiple cascading levels of inheritance at the machine, IIS, project, and folder level locations, with each providing a higher degree of specificity.

If you're getting this error, it means you've either:

  1. Added the same key twice in the same file (unlikely since you would've seen it)
  2. The same key already exists in a separate file higher up the inheritance chain

There can be a lot of different root causes for #2, but if you want to side step them, you can just remove any previous declarations and then re-add your own at that level (I'd pay good money for an upsert feature).

So just add <remove> tags like this for any offending elements:

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">

    <remove name="ErrorLog" />
    <remove name="ErrorMail" />
    <remove name="ErrorFilter" />

    <add name="ErrorLog"    type="Elmah.ErrorLogModule, Elmah"    preCondition="managedHandler" />
    <add name="ErrorMail"   type="Elmah.ErrorMailModule, Elmah"   preCondition="managedHandler" />
    <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />

  </modules>
</system.webServer>
1
  • 1
    This worked perfectly in my case. Just added the remove tag and worked perfectly. Thanks. Commented Sep 17, 2020 at 14:29
13

If you create an IIS website with the physical path the same as the project folder, and then use it create a virtual folder for the project, you are going to see this issue.
Your Web.config file is being loaded twice.
Instead as orjanto pointed out, create an empty folder and point the IIS website to it and then use it to create a virtual folder from Visual Studio.

2
  • Thanks you fixed it for us.
    – radbyx
    Commented May 20, 2016 at 9:14
  • 1
    Perfect, now I understand why it loads twice! Commented Jan 21, 2020 at 7:07
3

I had the same problem. It turned out that I had a different project that uses IISExpress with the same port number. Once I changed the project to use a different port number, the error went away.

1
  • consider adding more information in your answer to improve its quality
    – Inder
    Commented Jul 25, 2018 at 19:02
3

What worked for me. I deleted the {projectFolderParent}/.vs/config (you have to "show hidden folders/files)

Showing Hidden Items

After deleting that, I restarted my computer.

1
  • 1
    This is what worked for me after the framework upgrade.
    – h-rai
    Commented Mar 26 at 3:22
1

My Project Url in Web tab inside project properties pointed to wrong url and I was not able to change it ("Would you like to create Virtual Directory?" and VS was locked in a loop (Yes -> Unable to create, No -> Operation Canceled) until I manually reverted Url to the (incorrect) state it was before. The solution was to run VS as Admin. I was able to change that url and everything started to work. Hope it helps!

1

You may find that after upgrading to the latest windows 10 service pack June 2017, this issue is because certain nodes seem to now appear under the root config (machine.config). I removed my duplicates from web.config and it all worked again.

1

I had a problem of same type. Running my site published on IIS, the same error was shown.

I saw a web.config file in the wwwroot folder (out of application folder). I removed this file and the application ran OK.

Look for a web.config file placed in a wrong folder (above your folder application).

Good luck!

1

The answer that @orjanto posted solved the problem for me, but I had an additional problem on top of that. After fixing, Internet Explorer still thought that my HTML page was a directory instead of a file.

I had duplicates in my IIS Express config file:

<site name="MyAPI" id="56">
     <application path="/" applicationPool="Clr4IntegratedAppPool">
          <virtualDirectory path="/" physicalPath="C:\source\repos\MyAPI" />
     </application>
     <application path="/login.html" applicationPool="Clr4IntegratedAppPool">
          <virtualDirectory path="/" physicalPath="C:\source\repos\MyAPI" />
     </application>
     <bindings>
          <binding protocol="http" bindingInformation="*:57018:localhost" />
          <binding protocol="https" bindingInformation="*:44302:localhost" />
     </bindings>
 </site>

Note the path="/login.html" in the second <application> entry.

My page was redirecting to "localhost:57018/login.html/" like it was a directory.

Removing the second entry fixed the problem with the configuration file duplicates, however I continued to have a problem where Internet Explorer seemed to think that /login.html/ was a directory (Internet Explorer 11). To diagnose the problem, I checked Chrome and Chrome worked fine.

I went into Internet Explorer, went to Tools > Internet Options > General Tab. Then under "Browsing History" section, I deleted History, Cookies and Website Data, & Temp. Internet Files.

1

I tried all of the above methods and nothing worked for me. I was keep on getting the error something like

Cannot add duplicate collection entry of type 'add' with unique key attribute 'name' set to 'QueryStringModule'

At last I changed my server from

IIS Express to Local IIS in the project property build as given below.

enter image description here

and it worked for me.

-1

I had the same problem, here's what I did:

  • I changed my Server settings from Local IIS to IIS Express:

enter image description here

  • If it still doesn't work, I changed the port number to different port number then click the Create Virtual Directory button. Clean the solution then Rebuild.

enter image description here

-2

Just Change the port number which is not yet used by another application and it works

1
  • for completion, it'll help if you share "how to know if a port number is not used by another application" Commented Jan 16, 2019 at 4:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.