IIS express: Cannot add duplicate collection entry

2019-02-02 21:15发布

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?

12条回答
Lonely孤独者°
2楼-- · 2019-02-02 21:42

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>
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-02-02 21:42

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.

查看更多
Luminary・发光体
4楼-- · 2019-02-02 21:46

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.

查看更多
三岁会撩人
5楼-- · 2019-02-02 21:47

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!

查看更多
老娘就宠你
6楼-- · 2019-02-02 21:48

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.

查看更多
小情绪 Triste *
7楼-- · 2019-02-02 21:49

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.

查看更多
登录 后发表回答