公司更名了,换了logo。所以原来的logo和公司名称都要换掉。但是有个需求,对以前的老客户,又不想换。所以需要根据登录的用户,动态的显示不同的图片和公司名称。以前的图片名和公司名称都是些死在各个页面的html里面的。如果一个一个的去改,加到资源文件里面,或者用代码动态的获得图片公司名称,都需要大量的改动所有相关页面的代码。所以想问问大家,有没有什么好办法,比如是不是可以在页面产生HTML呈现到用户浏览器以前,过滤相应的图片名称和公司名称,从而实现动态的效果?也就是说,在所有的页面呈现以前,加一个过滤功能,不知道是不是可行,有没有兄弟做过类似的解决方案,能否提供一下实现。谢了
没有应用到母版页么。
using System.Collections.Generic;
using System.Text;
using System.Web; namespace K2046.Handlers
{
public class FilterModule : IHttpModule
{
public void Dispose()
{
// Do Nothing.
} public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(Application_BeginRequest);
} void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
if (true)//这里看情况加上你的条件,要不然所有的请求都会被处理,会造成其它文件类型的输出异常.
{
app.Response.Filter = new ResponseFilter(app.Response.Filter);
}
}
#endregion
}
} web.Config的<system.web>中再加上如下代码:<httpModules>
<add name="ResponseFilter" type="K2046.Handlers.FilterModule, K2046.Handlers"/>
</httpModules>esponseFilter的类,这个也是自己实现的,代码如下:using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions; namespace K2046.Handlers
{
public class ResponseFilter : Stream
{
private Stream _StreamFilter; private ResponseFilter() { } public ResponseFilter(Stream stream)
{
_StreamFilter = stream;
} public override bool CanRead
{
get { return true; }
} public override bool CanSeek
{
get { return true; }
} public override bool CanWrite
{
get { return true; }
} public override void Flush()
{
_StreamFilter.Flush();
} public override long Length
{
get
{
return _StreamFilter.Length;
}
} public override long Position
{
get
{
return _StreamFilter.Position;
}
set
{
_StreamFilter.Position = value;
}
} public override int Read(byte[] buffer, int offset, int count)
{
return _StreamFilter.Read(buffer, offset, count);
} public override long Seek(long offset, SeekOrigin origin)
{
return _StreamFilter.Seek(offset, origin);
} public override void SetLength(long value)
{
_StreamFilter.SetLength(value);
} public override void Write(byte[] buffer, int offset, int count)
{
string Html = Encoding.UTF8.GetString(buffer);
//你可以在这里干活了,做判断,然后判断是显示哪个logo
if(判断如果应该输入新的LOGO
//Html = Regex.Replace(Html, @">\s+?<", "><");
Html=Html.Replace("olologo.gif","newlogo.gif");
buffer = Encoding.UTF8.GetBytes(Html);
_StreamFilter.Write(buffer, offset, buffer.Length);
}
}
}
用户登陆后转不同的页面就行了.但登陆的页面LOGO你怎么用?
呵呵,我明白这个也由不得我们,看了楼上的回答,我觉得自定义HTTP管道的那个方法应该可行
public class ImageHandler : IHttpHandler
{
public bool IsReusable
{
get { return true; }
} public void ProcessRequest(HttpContext context)
{
处理代码
}
}
然后在web.config中加配置
<httpHandlers>
<add path="logo.gif(图片名称)" verb="*" type="(有命名空间就加上)ImageHandler"></add>
ASP.NET 案例分享:因应多站台透过 IHttpHandler 动态切换图片水印
判断逻辑像你所说要改用 Session,所以这一篇也可以参考一下:
HttpHandler 中使用 Session 的注意事项其實个人觉得棘手的是公司名称的部分...
using System.Collections.Generic;
using System.Web;/// <summary>
///PicHandler 的摘要说明
/// </summary>
namespace CustomerHandler
{
public class PicHandler:IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string FileName = context.Server.MapPath(context.Request.FilePath);
context.Response.ContentType = "image/JPEG";
if (context.Request.Cookies["XX"].ToString() == "old")
{
context.Response.WriteFile(FileName);
}
else
{
context.Response.WriteFile("newlogo.jpg");
}
} public bool IsReusable
{
get { return true; }
}
}
}<httpHandlers>
<add path="oldlogo.jpg" verb="*" type="CustomerHandler.PicHandler,PicHandler"/>
</httpHandlers>刚学,看行不行。
页面过滤可使用HttpHandler