C# 如何使用ASP.NET 4.0 URL重写功能重写URL?
我在ASP.NET 4.0中构建了一个应用程序。我需要在Globax.asax文件中使用url重写类重写url,或者在IIS 7或IIS 7.5中使用microsoft url重写扩展名重写url 例如 我有一个动态构建的url,不幸的是我无法更改,因为它是第三方控制的C# 如何使用ASP.NET 4.0 URL重写功能重写URL?,c#,asp.net,regex,.net-4.0,url-rewriting,C#,Asp.net,Regex,.net 4.0,Url Rewriting,我在ASP.NET 4.0中构建了一个应用程序。我需要在Globax.asax文件中使用url重写类重写url,或者在IIS 7或IIS 7.5中使用microsoft url重写扩展名重写url 例如 我有一个动态构建的url,不幸的是我无法更改,因为它是第三方控制的 http://sitename.com/store/description/product?table=page2 我需要把它改写成 http://sitename.com/store/description/product?
http://sitename.com/store/description/product?table=page2
我需要把它改写成
http://sitename.com/store/description/product?id=2
试试这个:
string data = @"http://sitename.com/store/description/product?table=page2";
string pattern = @"(table)(?:=)([^\d]+)";
Console.WriteLine ( Regex.Replace(data, pattern, "id="));
// Result
// http://sitename.com/store/description/product?id=2
当我需要做一些重写来伪造子域的想法时,我发现了这一点。以下代码通常位于web.config文件中,也可以通过IIS7 management studio进行设置
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
<rewrite>
<rules>
<clear />
<!-- Ameritexintl Website Publisher -->
<rule name="ameritexintl-Web-publisher" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www\.publisher\.ameritexintl\.com$" />
</conditions>
<action type="Redirect" url="http://publisher.ameritexintl.com/{R:0}" />
</rule>
<rule name="ameritexintl-Web-publisher-rewrite" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www\.)?publisher\.ameritexintl\.com$" />
<add input="{PATH_INFO}" pattern="^/publisher/($|/)" negate="true" />
</conditions>
<action type="Rewrite" url="/publisher/{R:0}" />
</rule>
</rules>
</rewrite>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
有一系列屏幕截图,显示当您正在查看和设置url重写规则时IIS对话框的外观
希望这对你有所帮助,祝你的项目好运。@Eugene,你喜欢使用IIS\u ISAPI吗?
如果是,你可以试试这个
如果没有,您可以使用此url重写模块
如果您想编写自己的代码,则需要实现httpModule接口
和HttpContent.RewritePath
方法
例如:
public sealed class RewriterHttpModule : IHttpModule
{
private static RewriterEngine _rewriter = new RewriterEngine();
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(BeginRequest);
}
private void BeginRequest(object sender, EventArgs e)
{
var context=((HttpApplication)sender).Context;
string path = context.Request.Path;
/*
url rewrite list:
Dictionary<string,string>
*/
Dictionary<string, string> urls = new Dictionary<string, string>();
urls.Add(@"/store/description/product?table=page(\d+)", "/store/description/product?id=$1");
foreach (var pair in urls)
{
if (Regex.IsMatch(path, pair.Key))
{
var newUrl = Regex.Replace(path, pair.Key, pair.Value);
//rewrite url
context.RewritePath(newUrl, false);
}
}
}
}
公共密封类重写RhttpModule:IHttpModule
{
私有静态重写引擎_rewriter=new RewriterEngine();
公共空间处置()
{
}
公共void Init(HttpApplication上下文)
{
context.BeginRequest+=新事件处理程序(BeginRequest);
}
私有void BeginRequest(对象发送方,事件参数e)
{
var context=((HttpApplication)sender.context;
字符串路径=context.Request.path;
/*
url重写列表:
字典
*/
字典URL=新字典();
添加(@/store/description/product?table=page(\d+),“/store/description/product?id=$1”);
foreach(URL中的变量对)
{
if(Regex.IsMatch(path,pair.Key))
{
var newUrl=Regex.Replace(路径,pair.Key,pair.Value);
//重写url
重写路径(newUrl,false);
}
}
}
}
regex部件工作正常。您知道如何在IIS 7或Global.asax中实现它吗?自从我使用asp.net(Silverlight developer now)以来已经有一段时间了,我也不知道如何在IIS 7中实现它。对不起,谢谢,但这不是我的问题的解决办法。我需要重写动态url,你可以在我的示例中看到,我猜你真的只需要从OmegaMan的示例中获取正则表达式,并将其作为模式来匹配路径信息值。你能提供一个示例吗?