在MCV3中发送HTTP标头后无法重定向

时间:2012-12-26 05:52:45

标签: asp.net-mvc-3

我使用jQuery $.ajax调用来获取mvc3中的局部视图。同时我使用sessionfilter来检查会话超时。一旦会话超时,它将返回登录页面,但登录页面将显示在部分页面div中,而不是重定向到整个页面。我在global.ascx页面上尝试了下面的代码来修复它,但是它没有用。它会引发以下错误:

public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
   filterContext.HttpContext.Response.Clear();
   filterContext.HttpContext.Response.Write("**custom error message**");
   filterContext.HttpContext.Response.StatusCode = 401;
   filterContext.HttpContext.Response.End();
}

例外如下

 at System.Web.HttpResponse.Redirect(String url, Boolean endResponse, Boolean permanent)
 at System.Web.HttpResponseWrapper.Redirect(String url, Boolean endResponse)
 at TransFlow.SessionExpireFilterAttribute.OnAuthorizationHelp(ActionExecutingContext filterContext) in D:\LocalSVN\INTELLAFLO\5_Source\branches\balasubramanian\TransFlow\TransFlow\Global.asax.cs:line 131
 at TransFlow.SessionExpireFilterAttribute.OnActionExecuting(ActionExecutingContext filterContext) in D:\LocalSVN\INTELLAFLO\5_Source\branches\balasubramanian\TransFlow\TransFlow\Global.asax.cs:line 120
 at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
 at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14()
 at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
 at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14()
 at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
 at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
 at System.Web.Mvc.Controller.ExecuteCore()
 at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
 at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
 at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.<BeginProcessRequest>b__5()
 at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
 at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
 at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
 at System.Web.Mvc.MvcHandler.<>c__DisplayClasse.<EndProcessRequest>b__d()
 at System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f)
 at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)
 at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
 at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
 at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
 at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

1 个答案:

答案 0 :(得分:0)

您需要从操作过滤器或控制器执行重定向,而不是直接写入响应。这样可以避免您看到的页面布局问题。