下载地址:http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi,因为要节省时间,而且理解了一般问题就不大了。 使用起来也很简单,只需要以下几步配置。
在web.config
<configuration>
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>
</configSections>
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/test/(\w{0,20})</LookFor>
<SendTo>~/test.aspx?tag=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
</configuration>
<modules>
<add name="ScriptModule" preCondition="integratedMode"
type="System.Web.Handlers.ScriptModule, System.Web.Extensions,
</modules>
匹配是用正则表达式的,所以还是得花点时间学下,之前一直懒,能不用就不用,看来还是逃不了,必须学哈。
不过在页面中如果有updatepanel或者async postback就会出现地址错误的情况,这个在老赵的文章里也有提到。
最简单的解决办法在该from里添加
Sys.Application.add_load(function()
{
var form = Sys.WebForms.PageRequestManager.getInstance()._form;
form._initialAction = form.action = window.location.href;
});
但这样就是有用到的页面都要写比较麻烦,而且还会把实际地址暴露给客户端
所以另一种解决办法是使用Control Adaptor来改变Form生成时的行为。原文地址:http://www.cnblogs.com/JeffreyZhao/archive/2008/01/13/Url-Rewrite-3.html
namespace ajaxURLRewrite
...{
public class FormRewriterControlAdapter :
System.Web.UI.Adapters.ControlAdapter
...{
protected override void Render(HtmlTextWriter writer)
...{
base.Render(new RewriteFormHtmlTextWriter(writer));
}
}
public class RewriteFormHtmlTextWriter : HtmlTextWriter
...{
public RewriteFormHtmlTextWriter(HtmlTextWriter writer)
: base(writer)
...{
this.InnerWriter = writer.InnerWriter;
}
public RewriteFormHtmlTextWriter(TextWriter writer) : base(writer)
...{
this.InnerWriter = writer;
}
public override void WriteAttribute(string name, string value, bool fEncode)
...{
if (name == "action")
...{
HttpContext context = HttpContext.Current;
if (context.Items["ActionAlreadyWritten"] == null)
...{
value = context.Request.RawUrl;
context.Items["ActionAlreadyWritten"] = true;
}
}
base.WriteAttribute(name, value, fEncode);
}
}
}
不过要让这个Control Adaptor生效,还必须在Web项目中创建一个browser文件,例如“App_Browsers\Form.browser”,在里面写入如下代码:
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.HtmlControls.HtmlForm"
adapterType="ajaxURLRewrite.FormRewriterControlAdapter" />
</controlAdapters>
</browser>
</browsers>
这样这个问题就完美解决了