'C# MVC - Losing Virtual Directory when using RedirectToRouteResult on session time out
I'm building an MVC 4.5.2 application and am having problems with the virtual path getting lost using RedirectToRouteResult. I've created a SessionTimeout Filter Attribute that checks to see if session variables are expired and consequently redirecting to the "Home" controller and "Index" action. The app is running in a virtual directory say example.com/AppBeta. My Controllers are decorated with the SessionTimeout filter attribute but instead of redirecting to example.com/AppBeta/Home it redirects to example.com/Home once the session is empty, causing a 404 error.
How can I use RedirectToRouteResult without losing my virtual directory path?
public class SessionTimeoutFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpContext ctx = HttpContext.Current;
HttpSessionStateBase session = filterContext.HttpContext.Session;
// If the browser session or authentication session has expired...
if (session.IsNewSession || ctx.Session["ValidSession"] == null)
{
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
// Setting statuscode to internal server error should trigger ajaxError at client
filterContext.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
// in case of response is success at client - inform calling JavaScript code that a user should be redirected.
var result = new JsonResult;
result.Data = "SessionInternalServerError";
result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
filterContext.Result = result;
//filterContext.Result = new JsonResult { Data = "SessionInternalServerError" };
}
else
{
// For round-trip requests.
// Redirect to Home which will redirect to Users or Inventory controller base on role
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary {
{ "Controller", "Home" },
{ "Action", "Index" }
});
}
}
base.OnActionExecuting(filterContext);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
