How to ignore some route when using ASP.NET friendly URLs?

I have been using ASP.NET friendly URLs with success, but I need to ignore the route for a specific Foo.aspx page (since this page needs POST data and after re-routing, POST data is no longer available in Page_Load() !).

Using ASP.NET friendly URLs seems to discard any attempts to ignore the route. Even the MSDN example to ignore the route does not work when ASP.NET friendly URL routing is used:

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

And to ignore the route to Foo.aspx , the code should look like this, right?

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

The Global.asax code is as follows:

 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 was asked at the ASP.NET Friendly codeplex URLs but received no response.

Thanks for your help on this :)

+6
source share
2 answers

Thanks to a comment by Damian Edwards, I solved this problem completely, thanks Damian.

I just need to get from WebFormsFriendlyUrlResolver to override the ConvertToFriendlyUrl() method to make it non-op when the URL matches the URL that 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 code now looks like this:

 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); } 
+8
source

It was interesting - I had to redo :) 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 said (re: physical file), but rather, not causing the redirect in the first place.

This will crash (redirect, POST data loss):

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

This will be fine (you will receive POST data, there will be no redirection):

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

Difference? I do not ask FriendlyUrls to “redirect” anything in the second option. In the first version, I request the file "aspx", so FriendlUrls faithfully performs its task in order to be and "process" it (and perform constant redirection to the "friendly URL", which is GET, and everything goes POSTed).

  • Check the request in the 1st option ( target.aspx ): target.aspx request
  • Check the request in the second option (without limitation, purpose ): enter image description here

It was a clue:

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

And he does what he says, "makes a permanent redirect" (to the "friendly URL") ... when "necessary" (if the "aspx" file is requested). You can tinker with this with any page on the WebForms website.

  • if you request foo.aspx you will see a redirect (before foo )
  • if you request foo , redirect

You can also comment

 settings.AutoRedirectMode = RedirectMode.Permanent; 

and everything will work, but the FriendlyUrls target seems to hit ...

Thinking about it, it makes sense. There is no need to redirect for each request (ugh for performance), but only if / when it is necessary ...

Hth ....

+2
source

Source: https://habr.com/ru/post/974244/


All Articles