在iis上的另一个web应用程序内部时,在WebAPI上接收404

本文关键字:WebAPI 应用程序 iis 另一个 web 内部 | 更新日期: 2023-09-27 17:52:46

我创建了一个在IIS中托管的WebAPI(2.2(项目,从一个空白的Web项目开始,然后添加了WebAPI控制器及其依赖项,这是一个非常简单的项目,有一个控制器和几个委派处理程序。

问题是,每当我将该应用程序设置为其自己的IIS网站(IIS网站指向项目的根(,或者设置为根上没有应用程序的网站内的web应用程序(比如inetpub''wwwroot(时,API运行得很好,但每当我尝试将其作为根上有应用程序的网页内的web程序托管时,则对API的任何调用都会返回404 Not Found,并且IIS错误表明该调用由"StaticFile"处理程序处理,即使我在web.config中为路径"*"添加ExtensionlessUrlHandler时也是如此。

    + Sites
    |
    --+ WebApp1 (points to c:'apps'webapp1, a .Net web application)
      |
      --+MyAPI (ponts to c:'apps'MyAPI, the API in question) DOESN'T WORK
    |
    |
    --+ DEV (points to c:'inetpub'wwwroot, an empty folder)
      |
      --+MyAPIAgain (points to  c:'apps'MyAPI, the API in question) WORKS
    |
    |
    --+MyApiWebSite (points to  c:'apps'MyAPI, the API in question) WORKS

我已经在谷歌上搜索了这个问题,我找到的解决方案可以归结为两个:在iis中注册.net(事实并非如此,因为api已经在一个设置中运行,这将阻止MyAPIAgain和MyApiWebSite工作(,以及在web.config 中使用以下内容覆盖处理程序

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%'Microsoft.NET'Framework'v4.0.30319'aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%'Microsoft.NET'Framework64'v4.0.30319'aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    <modules>
      <remove name="UrlRoutingModule-4.0" />
      <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
    </modules>
  </system.webServer>

他们都没有成功。有什么想法吗?

在iis上的另一个web应用程序内部时,在WebAPI上接收404

使用以下标记包围根web.config文件的以下部分。(不要对运行时system.transactionsconfigSections部分执行此操作(如果适用,您的web.config文件中可能没有所有这些部分。

<location path="." inheritInChildApplications="false">
   <appSettings>
      .....
   </appSettings>
</location>
<location path="." inheritInChildApplications="false">
   <system.web.webPages.razor>   
      .....
   </system.web.webPages.razor>
</location>
<location path="." inheritInChildApplications="false">
   <system.web>
      ......
   </system.web>
</location>
<location path="." inheritInChildApplications="false">
   <system.webServer>
     .....
   </system.webServer>
</location>

请尝试一下,并告诉我它是否对您有效。

1( 在App_Start文件夹中添加一个公共静态类,如下所述:

public static class WebApiConfig
{
    /// <summary>
    /// Registers the specified configuration.
    /// </summary>
    /// <param name="config">The configuration.</param>
    public static void Register(HttpConfiguration config)
    {
        var container = UnityConfig.GetConfiguredContainer();
        // Web API routes
        config.MapHttpAttributeRoutes();
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
        //config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
        //config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
    }
}

在routeTemplate参数中设置API文件夹的正确路径

2( 在global.asax.cs中添加行(注意该行应放在RouteConfig之前(:

GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);

3( 测试您的API。

谨致问候。