Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc Asp.net mvc 2-从ActionFilter重定向时出错?_Asp.net Mvc - Fatal编程技术网

Asp.net mvc Asp.net mvc 2-从ActionFilter重定向时出错?

Asp.net mvc Asp.net mvc 2-从ActionFilter重定向时出错?,asp.net-mvc,Asp.net Mvc,mvc1和mvc2发生了什么变化?我有下面的代码,如果用户没有经过身份验证,它会重定向到登录页面。这不适用于mvc2,并导致“System.Web.HttpException:发送HTTP头后无法重定向” 堆栈轨迹如下所示: System.Web.HttpException: Cannot redirect after HTTP headers have been sent. at System.Web.HttpResponse.Redirect(String url, Boolean en

mvc1和mvc2发生了什么变化?我有下面的代码,如果用户没有经过身份验证,它会重定向到登录页面。这不适用于mvc2,并导致“System.Web.HttpException:发送HTTP头后无法重定向”

堆栈轨迹如下所示:

System.Web.HttpException: Cannot redirect after HTTP headers have been sent.
  at System.Web.HttpResponse.Redirect(String url, Boolean endResponse)
  at System.Web.HttpResponseWrapper.Redirect(String url, Boolean endResponse)
  at System.Web.Mvc.RedirectResult.ExecuteResult(ControllerContext context)
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
  at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass14.<InvokeActionResultWithFilters>b__11()
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
  at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass14.<>c__DisplayClass16.<InvokeActionResultWithFilters>b__13()
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
  at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
System.Web.HttpException:发送HTTP头后无法重定向。
在System.Web.HttpResponse.Redirect(字符串url,布尔值endResponse)
在System.Web.HttpResponseWrapper.Redirect(字符串url,布尔值endResponse)
位于System.Web.Mvc.RedirectResult.ExecuteSult(ControllerContext上下文)
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext ControllerContext,ActionResult ActionResult)
在System.Web.Mvc.ControllerActionInvoker.c_uuDisplayClass14.b_uuu11()中
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter筛选器、ResultExecutingContext预文本、Func`1 continuation)
在System.Web.Mvc.ControllerActionInvoker.c_uuDisplayClass14.c_uuDisplayClass16.b_uuu13()中
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext ControllerContext,IList`1 filters,ActionResult ActionResult)
位于System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext ControllerContext,String actionName)

您所要做的就是覆盖HandleUnauthorizedRequest而不是授权,只需将重定向结果url分配给AuthorizationContext.Result即可

base.on授权将检查身份验证,如果失败,将调用HandleUnauthorizedRequest

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
    string redirectOnSuccess = filterContext.HttpContext.Request.Url.AbsolutePath;
    string redirectUrl = string.Format("?ReturnUrl={0}", redirectOnSuccess);
    filterContext.Result = new RedirectResult(redirectUrl);
    return;
}

通常,您不会在授权中重定向。ASP.NET已经为您完成了这项工作。但是,同样,MVC也已经处理了授权,您可以使用或编写任何提供程序。在不知道你打算重写多少内容以及为什么要重写的情况下,很难说你应该做什么。一般来说,我认为开发人员过于急于重写ASP.NET身份验证,而且往往会做得不正确。授权失败时重定向的正确方法是在
web.config
中设置URI。在创建authorized属性之前,该站点最初是围绕mvc1 preview 2编写的。老实说,直到这个问题出现,我才意识到这个属性的存在。我会看一看,看看它是否解决了我的需要。太好了,这对我也有帮助。我有filterContext.Response.Redirect(redirectUrl),它似乎可以工作,但在后台抛出了错误。谢谢
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
    string redirectOnSuccess = filterContext.HttpContext.Request.Url.AbsolutePath;
    string redirectUrl = string.Format("?ReturnUrl={0}", redirectOnSuccess);
    filterContext.Result = new RedirectResult(redirectUrl);
    return;
}