开发者

BeginRequest event not fired in my httpmodule

开发者 https://www.devze.com 2023-02-14 01:57 出处:网络
I have an httpmodule, but since migrating the application .NET 4 have been experiencing some strane behaviour. The BeginRequest event is not fired on some开发者_JAVA百科 requests (but it does on other

I have an httpmodule, but since migrating the application .NET 4 have been experiencing some strane behaviour. The BeginRequest event is not fired on some开发者_JAVA百科 requests (but it does on others) - I haven't managed to work out a pattern as to when it is and isn't fired so far.

The EndRequest event always seems to be fired.

I found this article: http://connect.microsoft.com/VisualStudio/feedback/details/535999/beginrequest-event-is-not-raised-in-my-http-module-on-iis7

but the only suggestion from microsoft seems to be that an entry need to be made in the system.webServer/modules section - I already have that in place.

I'm quite new to .net and mvc - does anyone have any suggestions how I might track down the cause of this issue?

any help/suggestions much appreciated.


I spent quite some time trying to crack why it doesn't always fire! Here is the answer: You need an update panel around the request being called. So if you have a button for example that needs to call the begin and end request handlers then you place an Ajax update panel around the button and set the "UpdateMode" to "Always". This is due to the fact that these javascript events only fire before and after asynchronos postbacks. The Ajax update panel will provide the asynchronos call.

Example:

<script type="text/javascript" language="javascript">
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    function BeginRequestHandler(sender, args) {
        //Some code
    }
    function EndRequestHandler(sender, args) {
        //Some code
    }
</script>

<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel runat="server" ID="UpdatePanel6" UpdateMode="Always">
    <ContentTemplate>
        <asp:Button runat="server" ID="btnGo" Text="Go" OnClick="btnGo_OnClick"/>    
    </ContentTemplate>
</asp:UpdatePanel>

Please note that this is the correct methodology in asp.net. I'm not too sure about MVC. Hope that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号