gyhlw_dotnet/网站项目/VRS/QueryStringModule.cs

50 lines
1.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace VRS
{
public class QueryStringModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += Context_BeginRequest;
}
private void Context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
HttpRequest request = context.Request;
HttpResponse response = context.Response;
// 假设我们要处理的querystring参数名为"id"
string compress = request.QueryString["compress"];
if (!string.IsNullOrEmpty(compress))
{
// 这里可以添加处理id的逻辑
// ...
// 如果需要重定向到一个新的URL可以这样做
// response.Redirect("newurl?param=" + id);
// 如果需要修改querystring参数可以这样做
// context.RewritePath("page.aspx?param=" + id);
if ( !Int32.TryParse(compress,out int temp))
{
// var strURL = request.RawUrl + "&compress=" +"1";
var strURL = request.RawUrl.Replace("compress="+ compress, "compress=1");
response.Redirect(strURL);
}
}
}
public void Dispose()
{
// 清理代码,如果有必要的话
}
}
}