7

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()!).

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(/.*)?"});

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

routes.Ignore("{*fooaspx}", new { fooaspx = @"(.*/)?foo.aspx(/.*)?" });

The Global.asax code looks like:

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

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

Thanks for your help on this :)

2
  • Its my understanding that if a physical file exists, then it takes precedence (over any routing config), so why is it "re-routed"/redirected? Commented Aug 23, 2014 at 17:17
  • ASP.NET Friendly URLs code is doing its redirection job fine with all .aspx files that are physical files that exist, including the site\Foo.aspx. For example the url 'Site.com\Foo-Bar.aspx' gets redirected to 'site.com\foo-bar' and Foo-Bar.aspx file exists. How could this explain that routes.Ignore() doesn't work? Commented Aug 24, 2014 at 10:28

2 Answers 2

12

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

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

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);
}
Sign up to request clarification or add additional context in comments.

Comments

3

This was interesting - had to tinker :) In my comment above, what I was trying to say was "no need to ignore".

I was "right" and "wrong".

  • right: no need to ignore
  • wrong: not because of what I stated (re: physical file), but rather, by not invoking the redirect in the first place.

This will bomb out (redirect will occur, POST data is lost):

<asp:Button ID="btn1" runat="server" Text="Go" PostBackUrl="~/Target.aspx" />

This will be good (you will get POST data, no redirect occurs):

 <asp:Button ID="btn1" runat="server" Text="Go" PostBackUrl="~/Target" />

The difference? I'm not asking FriendlyUrls to "re-route" anything in the 2nd option. In the first option, I'm asking for an "aspx" file, so FriendlUrls will dutifully do its purpose for being and "handle" it (and do a permanent redirect to a "friendly url" which is a GET and there goes all the POSTed data).

  • Inspect request in 1st option (target.aspx): target.aspx request
  • Inspect request in 2nd option (extensionless, target): enter image description here

This was a clue:

var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;   

And it does what it says, "do a permanent redirect" (to a "friendly url")...when "necessary" (if an "aspx" file is requested). You can tinker with this with any page in your WebForms site

  • if you request foo.aspx you will see a Redirect (to foo)
  • if you request foo, no Redirect

You can also comment out

settings.AutoRedirectMode = RedirectMode.Permanent;

and things will work but sort of defeats the purpose of FriendlyUrls...

Thinking about it, it makes perfect sense. There is no need to "redirect" on every request (ugh for performance), rather only if/when necessary...

Hth....

2 Comments

Thanks! Your solution will be a nice Plan-B. The source that is posting-back to Site.com\Foo.aspx has the url hardcoded for now. We can change the source url to Site.com\Foo but this will come with a price. Still why the route.Ignore() doesn't work? Is there another redirection alternative? I tried also redirecting Foo.aspx to Foo in Web.config\system.webServer\rewrite\rules with no luck, the POST data get lost here too.
The ignore route doesn't work because our redirect logic happens in an HTTP Module, not in the route itself. This so we can handle redirects of default documents (e.g. Default.aspx). As to ignoring a specific path, this is likely possible if you derive from and customize WebFormsFriendlyUrlResolver and override a few methods, making them no-op when the path matches what you want to ignore. Then pass an instance in to the overload of EnableFriendlyUrls that accepts an IFriendlyUrlResolver as a parameter.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.