zoukankan      html  css  js  c++  java
  • 解决:无法在发送 HTTP 标头之后进行重定向。 跟踪信息: 在 System.Web.HttpResponse.Redirect(String url, Boolean endResponse, Boolean permanent) 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.<>……

    问题:在MVC的过滤器中验证用户状态时报如下错误:
     
    无法在发送 HTTP 标头之后进行重定向。 跟踪信息:   在 System.Web.HttpResponse.Redirect(String url, Boolean endResponse, Boolean permanent)
       在 System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)
     
    解决:
    在验证用户身份的时候首先要到第三方获取用户的openid,这时需要跳转到第三方页面,然后回到到当前页面链接,如果没有绑定则再次跳转到登录页面。过滤器代码如下:
     
    public class Logged : ActionFilterAttribute, IAuthorizationFilter
    {
        ………略………
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            string openid = XXXXXX.GetOpenId();//在获取openid时XXXXXX.GetOpenId()已经跳转,代码略
            
            if (string.IsNullOrEmpty(openid))
            {
                filterContext.Result = new RedirectResult("https://www.****.com/login");//在获取openid时页面已经跳转,这里就不要再次跳转了,否则报错:无法在发送 HTTP 标头之后进行重定向。
                return;
            }
            ………略………
        }
        ………略………
    }

     修改后的代码:

    public class Logged : ActionFilterAttribute, IAuthorizationFilter
    {
        ………略………
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            string openid = XXXXXX.GetOpenId();//在获取openid时XXXXXX.GetOpenId()已经跳转
            
            if (string.IsNullOrEmpty(openid))
            {
                filterContext.Result = new ContentResult();//终止Action继续向下执行
                return;
            }
            ………略………
        }
        ………略………
    }
     
     
     
     
     
  • 相关阅读:
    servlet技术学习随笔
    python使用openpyxl读取excel文件
    课后作业-阅读任务-阅读提问-3
    课后作业-阅读任务-阅读提问-2
    课后作业-阅读任务-阅读笔记3
    构建之法:现代软件工程-阅读笔记2
    《构建之法:现代软件工程-阅读笔记》
    结对编程需求分析
    团队-团队编程项目作业名称-需求分析
    团队-团队编程项目作业名称-成员简介及分工
  • 原文地址:https://www.cnblogs.com/jesselzj/p/6689028.html
Copyright © 2011-2022 走看看