簡體   English   中英

IIS 7.5上的Web API 2 URL路由403/404錯誤

[英]Web API 2 URL routing 403/404 error on IIS 7.5

我正在使用ASP.Net 4.5和Web Api 2屬性路由實現RESTful Web服務主機。 我還包括Web Api幫助頁面 當我在IIS Express上通過Visual Studio 2012在本地運行它時,它可以很好地工作,並且看起來非常酷。 它顯示帶有API菜單的默認/ home / index頁面,該菜單顯示了我所有RESTful API函數的文檔。 我還用SOAPUI測試了所有API方法,並且在本地運行一切正常。

但是,URL路由在部署到Windows 2008 Server R2上的IIS 7.5時絕對無法正常工作。

當我瀏覽該網站時,我得到:

403-禁止訪問:拒絕訪問。

如果我嘗試使用/ home / index或Web API方法的任何路由,則會得到:

404-找不到文件或目錄。

我已經在服務器上安裝了.Net 4.5,然后轉到Windows \\ Microsoft.NET \\ Framework64並運行:

Aspnet_regiis -i

IISRESET

我將應用程序部署到IIS,並將應用程序池設置為使用.Net Framework v4.0.30319和托管管道模式:集成。

我的web.config中具有以下設置:

    <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="None" />
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
    <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>
    </system.webServer>

我在web.config條目上嘗試了許多不同的變體。 例如, 這篇文章中的建議也不起作用:

<modules>
  <remove name="UrlRoutingModule-4.0" />
  <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
  <!-- any other modules you want to run in MVC e.g. FormsAuthentication, Roles etc. -->
</modules>

像這樣更改Handlers部分也沒有任何效果:

<handlers>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <remove name="WebDAV" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  <add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
</handlers>

我確實嘗試打開目錄瀏覽,然后顯示網站內容很好,所以403禁止錯誤是因為禁止目錄瀏覽。

但是我不想瀏覽內容,我希望URL路由能夠正常工作。 在RouteConfig.cs文件中,我具有默認路由:

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

因此,如果URL路由正常工作,則默認情況下應顯示“主頁/索引”視圖。

我還嘗試將Index.html文件添加到該站點,並且效果也很好。 因此,該網站可以像網站一樣正常運行,但是URL路由無法正常工作。

嘗試將此屬性添加到web.config中的<modules>標記中

runAllManagedModulesForAllRequests="true"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM