1、VS项目中添加FilterModule.cs来控制从登陆页面进入系统,该类代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
namespace VisualStudio
{
public class FilterModule : IHttpModule, IRequiresSessionState
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
//原因:这个事件时,Session尚未创建。要先指定类型在判断地址栏是否存在
//context.BeginRequest += new EventHandler(context_BeginRequest);
context.AcquireRequestState += (obj, e) =>
{
var app = (HttpApplication)obj;
var url = app.Request.RawUrl;
//还要先判断下请求类型
if (url.IndexOf(".aspx") > 0)
{
//判断非UserLogin请求且非saas平台请求 防止进入死循环(此网页包含重定向循环)
if (url.IndexOf("Login.aspx") < 0 )
{
if (app.Context.Session["UserLogin"] == null)
{
string loginURl = "";
string virPath = app.Request.ApplicationPath;
if (string.IsNullOrEmpty(virPath) || virPath == "/")
{
loginURl = string.Format("{0}://{1}:{2}/Login.aspx", app.Request.Url.Scheme, app.Request.Url.Host, app.Request.Url.Port);
}
else
{
loginURl = string.Format("{0}://{1}:{2}{3}/Login.aspx", app.Request.Url.Scheme, app.Request.Url.Host, app.Request.Url.Port, virPath);
}
app.Context.Response.Write("<script>alert('您尚未登录或者账户信息已过期,请重新登录!');top.location='" + loginURl + "' ;</script>");
app.Context.Response.End();
}
}
}
};
}
}
}
Web.config文件中添加配置节:
<system.web>
<httpModules>
<!--重写IHttpModule类,需要配置的信息-->
<add name="FilterModule" type="VisualStudio.FilterModule,VisualStudio" />
</httpModules>
<compilation debug="true" targetFramework="4.0" />
</system.web>
在项目中建立一个Login.aspx和Index.aspx,然后直接运行Index.aspx页面,会出现下图错误:

解决办法:
该错误提示已经很明显了:托管管道模式不正确
右键项目属性,找到左侧Web选项卡,将服务器中由原先的“使用本地IIS Web服务器”换成“使用Visual Studio开发服务器”,如下图所示:

===========================================================================
如果觉得对您有帮助,微信扫一扫支持一下:
