2014-09-05 37 views
7

背景的Application_BeginRequest沒有在PUT API調用IIS服務器

上激發我有一個MVC應用程序與組件的WebAPI。我使用jQuery和AJAX來傳遞和接收來自WebAPI的數據。我在每次調用期間都依靠Application_BeginRequest來執行一些平凡的任務。

問題

的Application_BeginRequest始終閃光的GET和POST調用到的WebAPI。但是,當我將PUT調用到WebAPI時,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

什麼管理管道模式在IIS中設置? – 2014-09-09 07:00:49

+0

託管管道模式設置爲集成,並且.Net Framework爲4.0。 – MarkBarciak 2014-09-09 13:16:48

+0

什麼版本的IIS? – 2014-09-09 19:34:29

回答

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將完全獨立,因爲它不應該與任何其他Web應用程序有關 - 至少這是我設計數據API的方式。

1

轉到IIS功能模式 - >單擊處理程序映射 - >滾動列表並找到無擴展名URL處理程序 - >檢查處理程序之前的所有處理程序,看看是否有其他東西註冊到處理程序PUT請求因此在遇到無擴展url處理程序之前劫持PUT。希望有幫助

相關問題