重写二级域名简单的方法

本文介绍了一个自定义HttpModule实现的简单域名解析方法。通过监听HTTP请求开始事件,根据域名前缀进行路径重写,实现不同子域名指向同一网站的不同页面组。此方法适用于ASP.NET Web应用程序。

一步范域名解析

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;

using System.Text;


namespace demohttpmodel
{

public class MyFirstHttpModule : IHttpModule
{

private void Application_BeginRequest(object sender, EventArgs e)
{

//HttpApplication application = (HttpApplication)sender;

//HttpContext context = application.Context;

//HttpRequest request = application.Request;

//HttpResponse response = application.Response;

string host = System.Web.HttpContext.Current.Request.Url.Host;
string domain = host.Split('.')[0];
if (domain != "www")
{
//System.Web.HttpContext.Current.Response.Write(System.Web.HttpContext.Current.Request.RawUrl + "<br>");
//System.Web.HttpContext.Current.Response.Write(System.Web.HttpContext.Current.Request.FilePath + "<br>");
//System.Web.HttpContext.Current.Response.Write(System.Web.HttpContext.Current.Request.Url + "<br>");
string tempurl = System.Web.HttpContext.Current.Request.RawUrl;
tempurl = tempurl.Substring(tempurl.LastIndexOf("/")+1);
tempurl = tempurl.IndexOf("?") > 0 ? (tempurl+"&") : (tempurl+"?");
// System.Web.HttpContext.Current.Response.Write(string.Format("/demo/{1}ID={0}", domain, tempurl) + "<br>");
System.Web.HttpContext.Current.RewritePath(string.Format("/Industry/{1}GroupID={0}", domain, tempurl));
}
//else
//{


// System.Web.HttpContext.Current.Response.Write(System.Web.HttpContext.Current.Request.RawUrl + "selex<br>");
//}
// System.Web.HttpContext.Current.Response.Write("<script>alert('demo')</script>");

// response.Write("我来自自定义HttpModule中的BeginRequest<br />");

}

private void Application_EndRequest(object sender, EventArgs e)
{

//HttpApplication application = (HttpApplication)sender;

//HttpContext context = application.Context;

//HttpRequest request = application.Request;

//HttpResponse response = application.Response;

//response.Write("我来自自定义HttpModule中的EndRequest<br />");

}

#region IHttpModule 成员

public void Dispose()

{ }

public void Init(HttpApplication application)
{

application.BeginRequest += new EventHandler(Application_BeginRequest);

application.EndRequest += new EventHandler(Application_EndRequest);

}

#endregion

}

}

<system.web>
<httpModules>
<add name="MyFirstHttpModule" type="demohttpmodel.MyFirstHttpModule,demohttpmodel"/>
<!--
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />-->
</httpModules>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值