我想用.NET的URL映射来实现如下跳转: http://127.0.0.1/test 映射到 http://127.0.0.1:88/tt.aspx?id=1
发布网友
发布时间:2022-05-05 00:20
我来回答
共1个回答
热心网友
时间:2023-10-09 05:46
urlmappings不能直接映射到其他端口的网站,不过你可以这样来实现:建立一个ASP.NET 模块
public class UrlMappingMole : IHttpMole
{
#region IHttpMole Members
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += context_BeginRequest;
}
void context_BeginRequest(object sender, EventArgs e)
{
System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("/aspnetTest");
UrlMappingsSection urlMappingSection = (UrlMappingsSection)config.GetSection("system.web/urlMappings");
UrlMappingCollection urlMappings = urlMappingSection.UrlMappings;
var host = HttpContext.Current.Request.Headers["Host"];
host = host.Remove(host.IndexOf(':'));
for (int i = 0; i < urlMappings.Count; i++)
{
if (string.Equals(urlMappings[i].Url, HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath))
{
string mappedUrl = urlMappings[i].MappedUrl;
if (mappedUrl.StartsWith("~"))
mappedUrl = mappedUrl.Substring(1);
mappedUrl = mappedUrl.Replace("127.0.0.1", host).Replace("localhost", host);
HttpContext.Current.Response.Redirect(mappedUrl);
break;
}
}
}
#endregion
}
在web.config里面配置:
<system.webServer>
<moles runAllManagedMolesForAllRequests="true" >
<add name="urlMappings" type="UrlMappingMole"/>
</moles>
</system.webServer>
<urlMappings enabled="false">
<add url="~/test" mappedUrl="
</urlMappings>
urlMappings里面必须要用这种形式,不能用~号,因为我没加判断,enabled要是false。
这样就可以跳转了。把这个配置在那个http://127.0.0.1/test的网站里,配置项如上方。