有一个很常见的需求,某个页面需要用户登录才能访问,或者某个操作需要用户登录

这就需要检测用户登录,一般是使用Ajax去检测是否登录,当用户未登录时跳转到登录页面

那么问题来了····

有的时候我们跳转到登录是直接Redirect,而有的时候因为是使用的Ajax,所以直接必须在客户端用Js使用location.href进行跳转

网上找了好久···找不到,然后想起Ext.Net实现了这个需求

就直接参考了Ext.Net的实现,也就是根据需求拦截Response.Redirect的跳转并转换为location.href

直接上代码

 public class AjaxHttpModule : IHttpModule
{
public void Dispose()
{
throw new NotImplementedException();
} public void Init(HttpApplication context)
{
context.PreSendRequestHeaders += context_PreSendRequestHeaders;
} void context_PreSendRequestHeaders(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
HttpContext context = application.Context;
if ((context.Response.StatusCode == 0x12e) && (context.Request.Headers.AllKeys.Contains("X-Requested-With")))
{
string redirectLocation = context.Response.RedirectLocation;
context.Response.ClearContent();
context.Response.StatusCode = ;
context.Response.ContentType = "text/html";
context.Response.Charset = "utf-8";
context.Response.Headers.Remove("location");
context.Response.Output.Write("{\"script\":\"window.location.href='" + redirectLocation + "';\"}");
}
}
}

代码很简单,当检测到响应到状态码为302并且是一个Ajax请求时,则自定义响应内容

将跳转的地址拼接为一个json字符串由客户端解析,最终解析结果为

{script:"window.location.href='/game/Play';"}

拦截Response.Redirect的跳转并转换为Js的跳转的更多相关文章

  1. 关于Response.Redirect 端口不一致的跳转

    如果内网和外网的端口号设置的不相同,那在使用Response.Redirect跳转的时候会无法成功.需要做以下设置: <system.web> <httpRuntime useFul ...

  2. aspx在页面跳转(Response.Redirect)时丢失session问题及解决办法

    [问题描述] 假设a.aspx.cs页面保存有Session["empid"]="3",当a.aspx.cs通过Response.Redirect(" ...

  3. Response.Redirect 无法跳转页面

    错误现象:Response.Redirect(Server.MapPath("BackIndex.aspx")); 打断点测试执行了这一句,Server.MapPath(" ...

  4. Response.Redirect("");Server.Transfer("")跳转页面的区别

      Response.Redirect("") Server.Transfer("") 转向其他站点 能 不能(只能站内转向) 是否可带QueryString参 ...

  5. 页面跳转 Server.Transfer和 Response.Redirect的区别

    1.Server.Transfer 用于把处理的控制权从一个页面转移到另一个页面,在转移的工程中没有离开服务器内部控件(如request,session等)保存的信息不变.因此你能从a页面跳转到b页面 ...

  6. asp.net知识汇总-页面跳转Server.Transfer和Response.Redirect

    1. Server.Transfer 服务器端跳转 webform1.aspx跳转到webform2.aspx页面 webform1.aspx代码如下: protected void Page_Loa ...

  7. 关于ASP.NET MVC中Response.Redirect和RedirectToAction的BUG (跳转后继续执行后面代码而不结束进程)以及处理方法

    关于ASP.NET MVC中Response.Redirect和RedirectToAction的BUG (跳转后继续执行后面代码而不结束进程)以及处理方法   在传统的ASP.NET中,使用Resp ...

  8. Response.Write("<script>alert('弹出对话框!')</script>") 后跟Response.Redirect("page.aspx");不能弹出对话框,直接跳转页面了 如何解?

    Response.Write和Response.Redirect一起用的时候就会这样,write脚本和redirect脚本不能同时使用,这样不会执行脚本,最好使用ClientScript 改进方法: ...

  9. Response.Redirect("x.aspx);跳转后session为null的解决方法

    通常我们做登陆的时候都是登录成功后为管理员保存一些信息,一般都会写类似下面的代码 if(登录成功) { Session["xx"] = "user"; Resp ...

随机推荐

  1. linux笔记2.24

    安装vsftpd mount /dev/cdrom /mnt cp vsftpd-1.1.3-8.i386.rpm /home/soccer/ chmod 777 vsftpd-1.1.3-8.i38 ...

  2. javascript解析从服务器返回的json格式数据

    在javascript中我们可以将服务器返回的json格式数据转换成json格式进行使用,如下: 1.服务器返回的json格式数据: 通过response.responseText获得: " ...

  3. C程序设计语言练习题1-17

    练习1-17 编写一个程序,打印长度大于80个字符的所有输入行. 代码如下: #include <stdio.h> // 包含标准库的信息. #define MAXROW 10 // 最大 ...

  4. 针对access数据库的增删改查

    1.执行查询操作:(ExecuteReader方法) string myConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data ...

  5. Android onActivityResult 设置requestCode 返回的code不对

    今天在项目里用到 Intent intent=new Intent(getActivity(), Test.class);startActivityForResult(intent, 1); onAc ...

  6. JVM虚拟机栈和本地方法栈溢出测试

    弄JAVA,那JVM,JAVA语法,JDK库,JAVAEE,流行框架是一个都不能少,才可以有全局感的. JVM高级特性这书,看得差不多了.慢慢实践. /** * * *VM Args: -Xms20m ...

  7. Xamarin.Forms-webservices访问

    虽然xamarin.android/ios 均支持右键添加web引用方式引用webservices,但是xamarin.forms不支持此方式,而如果android/ios各做一套采用抽象方式实现则工 ...

  8. HDOJ 1393 Weird Clock(明白题意就简单了)

    Problem Description A weird clock marked from 0 to 59 has only a minute hand. It won't move until a ...

  9. HDOJ 1303 Doubles(简单题)

    Problem Description As part of an arithmetic competency program, your students will be given randoml ...

  10. MSSQL 生成有意义的PROC

    MSSQL 生成有意义的PROC --MSSQL  用PROC 生成有意义的单号:如WP200011101 GO/****** 对象:  Table [dbo].[tbl_SequenceNum]   ...