Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore some route while using ASP.NET Friendly URLs?

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 :)

like image 419
Patrick from NDepend team Avatar asked Aug 23 '14 10:08

Patrick from NDepend team


People also ask

How do I disable friendly URLs?

If you want to enable Human-Friendly URLs, ensure that the urlFormat="humanfriendly" does exist as listed above. If you want to disable Human-Friendly URLs, ensure that urlFormat="humanfriendly" is removed from that section of the web.

What is friendly URL in asp net?

ASP.NET Friendly URLs is a library for ASP.NET Web Forms applications that enables developers to create URLs without file extensions for certain ASP.NET file types (such as . aspx and . ashx files).

How URL mapping and routing implemented in ASP net?

MVC applications use the ASP.NET routing system, which decides how URLs map to controllers and actions. When Visual Studio creates the MVC project, it adds some default routes to get us started. When you run your application, you will see that Visual Studio has directed the browser to port 63664.


2 Answers

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);
}
like image 128
Patrick from NDepend team Avatar answered Nov 08 '22 07:11

Patrick from NDepend team


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....

like image 25
EdSF Avatar answered Nov 08 '22 08:11

EdSF