2015-10-01 262 views
1

我正在使用ASP.Net 4.5和Web Api 2屬性路由實現RESTful Web服務主機。我還包括Web Api Help Pages。當我通過IIS Express上的Visual Studio 2012在本地運行它時,它非常棒,看起來非常酷;它會顯示默認的/ home/index頁面,並帶有一個API菜單,其中顯示了我所有RESTful API函數的文檔。我還用SOAPUI測試了所有的API方法,並且在本地運行了所有完美的工作。IIS 7.5上的Web API 2 URL路由403/404錯誤

但是,當部署到Windows 2008 Server R2上的IIS 7.5時,URL路由完全拒絕工作。

當我瀏覽該網站獲取:

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

如果我嘗試將/ home /指數或任何我得到我的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中的條目許多不同的變化。例如,在this post建議不起作用或者:

<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> 
    <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 Forbidden錯誤是因爲目錄瀏覽被禁止。

但我不想瀏覽內容,我希望URL路由工作。在RouteConfig.cs文件中我有一個默認路由:

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

所以如果URL路由工作,那麼它應該顯示默認的Home/Index視圖。

我也嘗試添加一個Index.html文件到網站,並且工作得很好。因此,該網站作爲一個網站正常工作,但URL路由拒絕工作。

+0

添加到<模塊>標籤你有沒有得到這個工作?我有類似的代碼和問題。 – DaveB

回答

0

嘗試這個屬性在web.config中

runAllManagedModulesForAllRequests="true"