背景
我有一个带有WebAPI组件的MVC应用程序。我使用jQuery和AJAX从WebAPI传递和接收数据。我依赖于在每次调用期间触发的Application_BeginRequest来执行一些平凡的任务。
问题
Application_BeginRequest始终触发对WebAPI的GET和POST调用。但是,当我对WebAPI进行PUT调用时,Application_BeginRequest会执行而不是,导致应用程序无法运行任务。实际上接收到PUT调用,因为正在执行控制器中的相应方法。
我在我的web.config文件中启用了PUT(如下所示)。如何启用PUT方法调用以触发Application_BeginRequest方法?
注意: Application_BeginRequest在使用IIS Express时会触发PUT调用,但在使用完整版本的IIS时则不会触发。
项目:完整项目可在此处下载:BeginRequestFail.zip
Web.Config中
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<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>
<httpErrors errorMode="Custom" existingResponse="PassThrough">
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="403" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="404" path="/Error/404" responseMode="ExecuteURL" />
<error statusCode="403" path="/Error/403" responseMode="ExecuteURL" />
<error statusCode="500" path="/Error/500" responseMode="ExecuteURL" />
</httpErrors>
<validation validateIntegratedModeConfiguration="false" />
<modules>
<remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
<remove name="WebDAVModule"/>
</modules>
<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>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
的Global.asax.cs
protected void Application_BeginRequest(object sender, EventArgs e) //Not triggered with PUT
{
Application["BeginRequest"] = _Scounter++;
}
答案 0 :(得分:1)
转到IIS功能模式 - &gt;单击Handler Mappings - &gt;滚动列表并找到Extensionless URL Handler - &gt;在该处理程序之前检查所有处理程序,看看是否有任何其他东西被注册到处理程序PUT请求,因此在它遇到无扩展url处理程序之前劫持PUT。希望有所帮助
答案 1 :(得分:0)
您的代码有效。不确定你的柜台,所以我改为:
public class MvcApplication : System.Web.HttpApplication
{
public static int BeginRequestCounter = 0;
protected void Application_BeginRequest(object sender, EventArgs e)
{
BeginRequestCounter++;
}
}
(删除其他代码以指出我更改的内容)
<p>@BeginRequestFail.MvcApplication.BeginRequestCounter</p>
请记住,当您执行AJAX调用时,页面上的计数器不会更新 - 在页面刷新后,您会看到计数器已正确递增。
不确定为什么要在Mvc项目中引用WebApi项目?我要么将它们分开,要么加入其中以避免任何怪异。就个人而言,我的WebApi将是完全独立的,因为它不应该与任何其他网络应用程序有任何关系 - 至少是我设计数据API的方式。