使用ASP.NET友好URL时如何忽略某些路由? [英] How to ignore some route while using ASP.NET Friendly URLs?

查看:210
本文介绍了使用ASP.NET友好URL时如何忽略某些路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在成功使用ASP.NET友好URL,但是我需要忽略特定 Foo.aspx 页面的路由(因为此页面需要POST数据,并且一旦重新路由的POST数据在 Page_Load()!中不再可用!)。

I am using ASP.NET Friendly URLs with success, but I need to ignore route for a particular Foo.aspx page (because this page needs POST data and once re-routed the POST data is not available anymore in Page_Load()!).

看起来就像在使用ASP。 NET友好URL放弃任何忽略路由的尝试。即使ASP忽略路由的 MSDN示例也无法正常工作使用.NET友好URL路由:

It looks like using ASP.NET Friendly URLs discard any attempt to ignore a route. Even the MSDN example for ignoring route doesn't work once ASP.NET Friendly URLs routing is used:

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);
}

此问题已在ASP.NET友好URLs Codeplex网站上被询问,但没有得到答案。

This question has been asked on the ASP.NET Friendly URLs codeplex site, but didn't get an answer.

感谢您的帮助对此:)

推荐答案

感谢达米安·爱德华兹(Damian Edwards)的评论,我完全解决了这个问题,谢谢达米安。

Thanks to Damian Edwards comment, I got this issue completely solved, thanks Damian.

我只需要从 WebFormsFriendlyUrlResolver 派生以覆盖方法 ConvertToFriendlyUrl()使其在网址与我不想重定向的网址匹配时不起作用:

I just need to derive from WebFormsFriendlyUrlResolver to override the method ConvertToFriendlyUrl() to make it no-op when the url match the url I don't want to redirect:

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中现在的代码如下:

Then in Global.asax the code now looks like:

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);
}

这篇关于使用ASP.NET友好URL时如何忽略某些路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆