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

查看:16
本文介绍了如何在使用 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 会放弃任何忽略路由的尝试.甚至用于忽略路由的 MSDN 示例一旦 ASP 也不起作用使用 .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 的路由,代码应该看起来像这样,不是吗?

And to ignore route to Foo.aspx the code should look like that, isn't it?

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 Friendly URLs codeplex 站点上提出,但没有得到答案.

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

感谢您对此的帮助:)

推荐答案

感谢 Damian Edwards 的评论,我完全解决了这个问题,感谢 Damian.

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

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

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天全站免登陆