0

I'm using ASP.NET 4.5 and have the following routing rule in my Global.asax file:

RouteTable.Routes.MapPageRoute("defaultRoute", "{*value}", "~/default.aspx")

What I'm trying to accomplish is redirecting dynamically generated URLs that are formatted like this:

http://myurl.com/firstnamelastname

Here is what one might actually look like:

http://myurl.com/davemackey

My problem is that the above redirects all requests - e.g. to axd or jpg files. Now I could add exclusions for every other type of file like so:

RouteTable.Routes.Ignore("{resource}.axd/*pathInfo}")

But this would be error prone and tedious (e.g., what happens if someone adds another file type to the project?).

So, what I'd like to do is something like this:

RouteTable.Routes.MapPageRoute("defaultRoute", "{*value}(where no suffix)", "~/default.aspx")

Or, put into my clear English:

If URL does not have a suffix, then redirect using defaultRoute to ~/default.aspx

Any thoughts on how to accomplish this?

==

Update:

I found this MSDN article. It seems that using Constraints might work to implement what I am speaking of above...but I'm not exactly sure how...

==

Update 2:

I've got a passable solution for the moment. I added the following:

RouteTable.Routes.Ignore("{path}/{value}")

Since image and other files are kept in sub-directories, this forces them to be excluded. Still, I have two concerns with this

  1. What if the path is longer than a single sub-directory, e.g. images/people/person.jpg?
  2. What if a file is placed into the main root (shouldn't be, but it could happen) that is a jpg or etc.?
Dave Mackey
  • 4,306
  • 21
  • 78
  • 136
  • 2
    Assuming you could consider a "." as a charcter that identifies a suffix you could make a RegEx constraint to check to see if the period exists. The example constraint from the MSDN article checks to make sure locale is 2 charcters a hyphen and two charcters and that year is a 4 digit number. – Nick Bork Oct 24 '12 at 14:19
  • 2
    And your regex for negative look-arounds is: ^((?!\.).)*$ as taken from the answer to the following question http://stackoverflow.com/questions/406230/regular-expression-to-match-string-not-containing-a-word I simply changed hede to \. – Nick Bork Oct 24 '12 at 14:26

0 Answers0