我成功使用ASP.NET友好URL,但我需要忽略特定Foo.aspx页面的路由(因为此页面需要POST数据,并且一旦重新路由POST数据在Page_Load()中不再可用!).
看起来使用ASP.NET友好URL会丢弃任何忽略路由的尝试.使用ASP.NET友好URL路由后,即使是忽略路由的MSDN example也不起作用:
routes.Ignore("{*allaspx}",new {allaspx=@".*\.aspx(/.*)?"});
并忽略到Foo.aspx的路由,代码看起来应该是这样,不是吗?
routes.Ignore("{*fooaspx}",new { fooaspx = @"(.*/)?foo.aspx(/.*)?" });
Global.asax代码如下所示:
public static void RegisterRoutes(RouteCollection routes) { // This doesn't work whether I put this code before or after ASP.NET Friendly URLs code. routes.Ignore("{*allaspx}",new { allaspx = @".*\.aspx(/.*)?" }); routes.Canonicalize().Lowercase(); var settings = new FriendlyUrlSettings(); settings.AutoRedirectMode = RedirectMode.Permanent; routes.EnableFriendlyUrls(settings); } void Application_Start(object sender,EventArgs e) { RegisterRoutes(RouteTable.Routes); }
这个问题has been asked on the ASP.NET Friendly URLs codeplex site,但没有得到答案.
谢谢你的帮助:)
解决方法
感谢Damian Edwards的评论,我完全解决了这个问题,感谢Damian.
我只需要从WebFormsFriendlyUrlResolver派生来覆盖方法ConvertToFriendlyUrl(),当url匹配我不想重定向的url时,使其成为no-op:
using Microsoft.AspNet.FriendlyUrls.Resolvers; public class MyWebFormsFriendlyUrlResolver : WebFormsFriendlyUrlResolver { public MyWebFormsFriendlyUrlResolver() { } public override string ConvertToFriendlyUrl(string path) { if (!string.IsNullOrEmpty(path)) { if (path.ToLower().Contains("foo")) { // Here the filter code return path; } } return base.ConvertToFriendlyUrl(path); } }
然后在Global.asax中,代码现在看起来像:
public static void RegisterRoutes(RouteCollection routes) { routes.Canonicalize().Lowercase(); var settings = new FriendlyUrlSettings(); settings.AutoRedirectMode = RedirectMode.Permanent; routes.EnableFriendlyUrls(settings,new IFriendlyUrlResolver[] { new MyWebFormsFriendlyUrlResolver() }); } void Application_Start(object sender,EventArgs e) { RegisterRoutes(RouteTable.Routes); }