简体   繁体   English

在 web.config 中设置位置路径会导致 404(IIS、ASP.NET Core)

[英]Setting location path in web.config results in 404 (IIS, ASP.NET Core)

We are migrating from ASP.Net to ASP.Net Core.我们正在从 ASP.Net 迁移到 ASP.Net Core。

We need to configure different authentications for different paths我们需要为不同的路径配置不同的认证

<location path="controllerpath/method">
    <system.webServer>
        <security>
            <authentication>
                <anonymousAuthentication enabled="true" />
            </authentication>
        </security>
    </system.webServer>
</location>

But as soon as we add the <location path="xyz"> entry the specified path is no longer reachable and the caller get's a 404 error.但是,一旦我们添加<location path="xyz">条目,指定的路径就不再可达,并且调用者得到 404 错误。 Even just putting即使只是放

<location path="xyz/abc">
</location>

Is enough to trigger the 404 response.足以触发404响应。 If I remove this entry is can be accessed again.如果我删除此条目,则可以再次访问。 The 404 comes from IIS directly, because even if the app fails to start (and all other calls result in a 503 for example), the path specified via location path still results in a 404. 404 直接来自 IIS,因为即使应用程序无法启动(例如,所有其他调用都会导致 503),通过位置路径指定的路径仍然会导致 404。

Here is the full web.config:这是完整的 web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <remove name="aspNetCore"/>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified"/>
      </handlers>
      <security>
        <requestFiltering>
          <requestLimits maxAllowedContentLength="52428800"/>
        </requestFiltering>
        <authorization>
          <add accessType="Allow" users="*" verbs="OPTIONS"/>
        </authorization>
      </security>
      <aspNetCore processPath="dotnet" arguments=".\assembly.dll" hostingModel="inprocess"> 
         <handlerSettings>
        <handlerSetting name="debugLevel" value="file" />
        <handlerSetting name="debugFile" value="c:\temp\ancm.log" />
     </handlerSettings>
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
      </environmentVariables>
      </aspNetCore>
      <httpProtocol allowKeepAlive="false">
        <customHeaders>
          <remove name="X-Powered-By"/>
        </customHeaders>
      </httpProtocol>
          <directoryBrowse enabled="true"/>
          <httpCompression>
            <dynamicTypes>
              <remove mimeType="*/*"/>
              <add mimeType="*/*" enabled="true"/>
            </dynamicTypes>
          </httpCompression>
        </system.webServer>
      </location>  
        <location path="xyz/abc" allowOverride="true">        
        </location>
    </configuration>
    <!--ProjectGuid: 29529648-A3F4-44CB-827B-B6B81B00A09C-->

I found the solution:我找到了解决方案:

the attribute inheritInChildApplications="false" made it that the handler was not registered for the other paths.属性inheritInChildApplications="false"使得处理程序没有为其他路径注册。 After removing the attribute (or changing the value to true) everything works as expected.删除属性(或将值更改为 true)后,一切都按预期工作。 I guess IIS then tried to locate the path as a resource.我猜 IIS 然后试图将路径定位为资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM