1

Given the following code, in my ASP.NET webform app;

void foo(RouteCollection routes){
 routes.MapPageRoute(string.Empty, "testroute", "~/hello.aspx", false);
}

The problem is, while http://localhost/testroute routes to hello.aspx, http://localhost/testroute/ also routes to hello.aspx.

Is there anyway to prevent this?

asked Nov 29, 2012 at 11:32
5
  • Do you have more routing rules? Or is it this single one? Commented Nov 29, 2012 at 11:35
  • Ah sorry the example use case I gave didnt make any sense, ive corrected it now. Commented Nov 29, 2012 at 11:42
  • Okay, that makes more sense, now! Why is this a problem, btw? The server actually won't treat URLs differently whether they have a trailing slash or not, normally; even if the URL is a file. For example, you could load /hello.aspx and /hello.aspx/ and get the same result. Commented Nov 29, 2012 at 11:53
  • Also, please be sure to use @AndrewBarber to respond to me, so I am notified. Or prefix anyone's name who has commented with an @ sign to notify them. We don't need to do that with you because we are responding to your post; the owner of a post that comments are posted to is automatically notified. Commented Nov 29, 2012 at 11:55
  • @AndrewBarber - thanks for the tip! It is for cosmetic reasons to an extent. Are you saying its not possible? Commented Nov 29, 2012 at 11:57

1 Answer 1

1

The best thing to do here, in my opinion, is simply to make sure your website uses consistent URLs in its <a> tags. Don't worry about extraneous paths into your routing mechanism, but instead concentrate on being sure to use only the desired URL pattern for your links.

For example, make sure your website does not contain any <a> tags with href="/testroute/", and it won't matter that it responds to that.

As I noted in the comments, it's actually standard that a trailing slash at the end of the path portion of a URL has no effect; you can include it, or not. This means two 'different' URLs can load the same page: /page.aspx and /page.aspx/ both load the same thing. But there's also another longstanding tradition here that is similar, with default documents; that is, / and /default.aspx will tend to load the same thing.

The solution in that case is, of course, the same as the solution here: Just be sure that your <a> tags use a consistent, single version of the URLs you want to use.


For the record, you could do something that detects the trailing slash and issues a 301 redirect, but I think it's much easier just to make sure you are consistent with the URLs.

answered Nov 29, 2012 at 12:03
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.