MVC 二级域名实现
时间:2010-12-10 来源:依晨
1.创建HttpHandler 处理
#region 二级域名处理
public class Domain_Url : IHttpHandler
{
public RequestContext RequestContext { get; private set; }
public Domain_Url(RequestContext Content)
{
this.RequestContext = Content;
}
public virtual void ProcessRequest(HttpContext context)
{
string url = context.Request.Url.ToString().ToLower();
url = url.Substring(url.IndexOf("http://") + 7);
string custom_Prefix = url.Substring(0, url.IndexOf('.'));
//服务器
if (custom_Prefix.Equals("www", StringComparison.CurrentCultureIgnoreCase))
{
context.Response.Redirect("/Home/Index");
context.Response.End();
return;
}
Tranlit.DAL.TranlitDB db = new Tranlit.DAL.TranlitDB();
var shop = db.Shop.FirstOrDefault(s=>s.ShopDomain == custom_Prefix);
if (shop != null)
{
context.Response.Redirect("/mall/shopIndex?shopId=" + shop.AutoId);
context.Response.End();
return;
}
context.Response.Redirect("/Home/Index");
var host = context.Request.Url.Host;
if (!(host.Equals("www.chuanlidamai.com", StringComparison.CurrentCultureIgnoreCase) ||
host.Equals("www.cl398.com", StringComparison.CurrentCultureIgnoreCase)))
host = "www.cl398.com";
}
public bool IsReusable
{
get { return false; }
}
}
public class DomainHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new Domain_Url(requestContext); //返回自定义的HttpHandler
}
}
#endregion
2.修改 Global.aspx.cs
routes.Add(new Route("", (new Tranlit.BLL.UrlRouting.DomainHandler())));