How to handle Application_BeginRequest using a custom filter in asp.net mvc?

Posted by denis_n on Stack Overflow See other posts from Stack Overflow or by denis_n
Published on 2010-05-24T19:16:17Z Indexed on 2010/05/24 19:21 UTC
Read the original article Hit count: 251

Filed under:
|
|
|

How to handle Application_BeginRequest using a custom filter in asp.net mvc?

I want to restore session only for one route (~/my-url).

I would be cool, if I could create a custom filter and handle that.

protected void Application_BeginRequest(object sender, EventArgs e)
    {
        var context = HttpContext.Current;

        if (string.Equals("~/my-url",
            context.Request.AppRelativeCurrentExecutionFilePath, 
            StringComparison.OrdinalIgnoreCase))
        {
            string sessionId = context.Request.Form["sessionId"];

            if (sessionId != null)
            {
                HttpCookie cookie = context.Request.Cookies.Get("ASP.NET_SessionId");
                if (cookie == null)
                {
                    cookie = new HttpCookie("ASP.NET_SessionId");
                }
                cookie.Value = sessionId;
                context.Request.Cookies.Set(cookie);
            }
        }

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about mvc