How to handle Application_BeginRequest using a custom filter in asp.net mvc?
I want to restore session only for one route (~/my-url).
It 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);
}
}