阅读量:0
在C#中实现多域名下的重定向,可以通过使用ASP.NET的URL Rewrite模块来实现。以下是一个示例代码:
using System; using System.Web; public class RedirectModule : IHttpModule { public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(this.context_BeginRequest); } private void context_BeginRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; string currentDomain = context.Request.Url.Host; if (currentDomain == "www.domain1.com") { context.Response.Redirect("http://www.domain2.com" + context.Request.Url.PathAndQuery); } else if (currentDomain == "www.domain3.com") { context.Response.Redirect("http://www.domain4.com" + context.Request.Url.PathAndQuery); } } public void Dispose() { } }
在以上示例代码中,创建了一个实现了IHttpModule接口的RedirectModule类,该模块在请求开始时会检查当前的域名,然后根据不同的域名进行重定向操作。可以根据实际需求修改重定向的逻辑和目标域名。最后,需要在web.config文件中配置使用该模块:
<configuration> <system.webServer> <modules> <add name="RedirectModule" type="Namespace.RedirectModule"/> </modules> </system.webServer> </configuration>
其中,Namespace为RedirectModule类所在的命名空间。这样就可以实现多域名下的重定向功能。