Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URLs with slash in parameter?

Question:

I am creating a wiki software, basically a clone of wikipedia/mediawiki, but in ASP.NET MVC (the MVC is the point, so don't recommend me ScrewTurn).

Now I have a question:

I use this route mapping, to route a URL like:
http://en.wikipedia.org/wiki/ASP.NET

        routes.MapRoute(             "Wiki", // Routenname             //"{controller}/{action}/{id}", // URL mit Parametern             "wiki/{id}", // URL mit Parametern             new { controller = "Wiki", action = "dbLookup", id = UrlParameter.Optional } // Parameterstandardwerte         ); 

Now it just occured to me, that there might be titles like 'AS/400':
http://en.wikipedia.org/wiki/AS/400

Incidentially, there is also this one (title 'Slash'):
http://en.wikipedia.org/wiki//

And this one:
http://en.wikipedia.org/wiki//dev/null

Overall, Wikipedia seems to have a list of interesting titles like this: http://en.wikipedia.org/wiki/Wikipedia:Articles_with_slashes_in_title

How do I make routes like this route correctly ?

Edit:
Something like:
If the URL starts with /Wiki/, and if it doesn't start with /wiki/Edit/ (but not /Wiki/Edit) then pass all the rest of the URL as Id.

Edit:
Hmm, just another problem: How can I route this one:
http://en.wikipedia.org/wiki/C&A

Wikipedia can...

Edit:
According to wikipedia, due to clashes with wikitext syntax, only the following characters can never be used in page titles (nor are they supported by DISPLAYTITLE):

# < > [ ] | { } 

http://en.wikipedia.org/wiki/Wikipedia:Naming_conventions_(technical_restrictions)#Forbidden_characters

Edit:
To allow * and &, put

<httpRuntime requestPathInvalidCharacters="" /> 

into section <system.web> in file web.config

(Found here: http://www.christophercrooker.com/use-any-characters-you-want-in-your-urls-with-aspnet-4-and-iis)

like image 611
Stefan Steiger Avatar asked Jun 13 '11 09:06

Stefan Steiger


People also ask

How do you handle a slash in a URL?

The addition of a slash at the end of a URL instructs the web server to search for a directory. This speeds the web page loading because the server will retrieve the content of the web page without wasting time searching for the file.

What is URL slash?

Historically, it's common for URLs with a trailing slash to indicate a directory, and those without a trailing slash to denote a file: http://example.com/foo/ (with trailing slash, conventionally a directory) http://example.com/foo (without trailing slash, conventionally a file) But they certainly don't have to.

Can path variable contain slash?

Unfortunately, we soon find out that this returns a 404 if the PathVariable contains a slash. The slash character is the URI standard path delimiter, and all that goes after it counts as a new level in the path hierarchy. As expected, Spring follows this standard.


1 Answers

You could use a catchall route to capture everything that follows the wiki part of the url into the id token:

routes.MapRoute(     "Wiki",     "wiki/{*id}",      new { controller = "Wiki", action = "DbLookup", id = UrlParameter.Optional } ); 

Now if you have the following request: /wiki/AS/400 it will map to the following action on the Wiki controller:

public ActionResult DbLookup(string id) {     // id will equal AS/400 here     ... } 

As far as /wiki// is concerned I believe you will get a 400 Bad Request error from the web server before this request ever reaches the ASP.NET pipeline. You may checkout the following blog post.

like image 68
Darin Dimitrov Avatar answered Oct 27 '22 08:10

Darin Dimitrov