简体   繁体   English

IIS 7.5上的Web API 2 URL路由403/404错误

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

I am implementing a RESTful web services host using ASP.Net 4.5 and Web Api 2 Attribute Routing. 我正在使用ASP.Net 4.5和Web Api 2属性路由实现RESTful Web服务主机。 I also included Web Api Help Pages . 我还包括Web Api帮助页面 When I run it locally through Visual Studio 2012 on IIS Express it works great and looks really cool; 当我在IIS Express上通过Visual Studio 2012在本地运行它时,它可以很好地工作,并且看起来非常酷。 it displays the default /home/index page with an API menu that shows the documentation for all my RESTful API functions. 它显示带有API菜单的默认/ home / index页面,该菜单显示了我所有RESTful API函数的文档。 I have also tested all the API methods with SOAPUI and running locally everything works perfectly. 我还用SOAPUI测试了所有API方法,并且在本地运行一切正常。

But the URL routing absolutely refuses to work when deployed to IIS 7.5 on Windows 2008 Server R2. 但是,URL路由在部署到Windows 2008 Server R2上的IIS 7.5时绝对无法正常工作。

When I browse the site I get: 当我浏览该网站时,我得到:

403 - Forbidden: Access is denied. 403-禁止访问:拒绝访问。

If I try the /home/index or any of the routes of my web api methods I get: 如果我尝试使用/ home / index或Web API方法的任何路由,则会得到:

404 - File or directory not found. 404-找不到文件或目录。

I have installed .Net 4.5 on the server and then went to Windows\\Microsoft.NET\\Framework64 and ran: 我已经在服务器上安装了.Net 4.5,然后转到Windows \\ Microsoft.NET \\ Framework64并运行:

Aspnet_regiis -i Aspnet_regiis -i

iisreset IISRESET

I deployed my app to IIS and set the Application Pool to use .Net Framework v4.0.30319 and Managed pipeline mode: Integrated. 我将应用程序部署到IIS,并将应用程序池设置为使用.Net Framework v4.0.30319和托管管道模式:集成。

I have the following settings in my web.config: 我的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>

I have tried many different variations on the web.config entries. 我在web.config条目上尝试了许多不同的变体。 For example, the suggestion in this post does not work either: 例如, 这篇文章中的建议也不起作用:

<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>

Nor does changing the Handlers section like this have any effect: 像这样更改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>

I did try turning directory browsing on and then it showed the website contents just fine, so the 403 Forbidden error is because directory browsing was forbidden. 我确实尝试打开目录浏览,然后显示网站内容很好,所以403禁止错误是因为禁止目录浏览。

But I don't want to browse the contents, I want URL routing to work. 但是我不想浏览内容,我希望URL路由能够正常工作。 In RouteConfig.cs file I have a default route: 在RouteConfig.cs文件中,我具有默认路由:

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

So if URL routing were working then it should show the Home/Index view by default. 因此,如果URL路由正常工作,则默认情况下应显示“主页/索引”视图。

I also tried adding an Index.html file to the site and that worked fine too. 我还尝试将Index.html文件添加到该站点,并且效果也很好。 So the website works fine as a website, but URL routing refuses to work. 因此,该网站可以像网站一样正常运行,但是URL路由无法正常工作。

尝试将此属性添加到web.config中的<modules>标记中

runAllManagedModulesForAllRequests="true"

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

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