URLs with a slash in the parameter?

Question:

I create wiki software, mainly a clone of wikipedia / mediawiki, but in ASP.NET MVC (this is MVC, so we don’t recommend ScrewTurn to me).

Now I have a question:

I use this route mapping to direct a URL, for example:
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 seemed to me that there might be names like "AS / 400":
http://en.wikipedia.org/wiki/AS/400

By the way, there is this one (the name is "Slash"):
http://en.wikipedia.org/wiki//

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

In general, Wikipedia has a list of interesting names: http://en.wikipedia.org/wiki/Wikipedia:Articles_with_slashes_in_title

How to make routes similar to this route?

Edit:
Something like:
If the URL starts with / Wiki /, and if it does not start with / wiki / Edit / (but not / Wiki / Edit) then pass the rest of the URL as an identifier.

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

Wikipedia may ...

Edit:
According to Wikipedia, due to clashes with wikitext syntax, only the following characters can never be used in page names (and they are not supported by DISPLAYTITLE):

 # < > [ ] | { } 

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

Edit:
To allow * and &, put

 <httpRuntime requestPathInvalidCharacters="" /> 

to the <system.web> section in the web.config file

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

+57
asp.net-mvc asp.net-mvc-routing
Jun 13 2018-11-11T00
source share
4 answers

You can use a common path 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 display the following action in the wiki controller:

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

Regarding /wiki// , I believe that you will get a 400 Bad Request error from the web server before this request ever reaches the ASP.NET pipeline. You can check out the following blog post .

+79
Jun 13 2018-11-11T00:
source share

@Darin: Well, this is obvious, the question is: why? controller + action + id are given, it seems to pass all this for routing again ... - Quandary Jun 13 '11 at 17:38

Quandry - you may already understand this, since your question is older than a year, but when you call RedirectToAction, you actually send an HTTP 302 response to the browser, which calls the browser to make a GET request to the specified action. Therefore, the infinite loop that you see.

See: Controller.RedirectToAction Method

+6
Jun 22 '12 at 20:24
source share

in Attribute Routing in mvc I had the same problem having / in the abc/cde in HttpGet

  [Route("verifytoken/{*token}")] [AllowAnonymous] [HttpGet] public ActionResult VerifyToken(string token) { //logic here } 

so you need to put * , because after that it will be considered a parameter

+1
Feb 13 '17 at 7:33
source share

As an option, still write to the Global.asax file:

  var uri = Context.Request.Url.ToString(); if (UriHasRedundantSlashes(uri)) { var correctUri = RemoveRedundantSlashes(uri); Response.RedirectPermanent(correctUri); } } private string RemoveRedundantSlashes(string uri) { const string http = "http://"; const string https = "https://"; string prefix = string.Empty; if (uri.Contains(http)) { uri = uri.Replace(http, string.Empty); prefix = http; } else if (uri.Contains(https)) { uri = uri.Replace(https, string.Empty); prefix = https; } while (uri.Contains("//")) { uri = uri.Replace("//", "/"); } if (!string.IsNullOrEmpty(prefix)) { return prefix + uri; } return uri; } private bool UriHasRedundantSlashes(string uri) { const string http = "http://"; const string https = "https://"; if (uri.Contains(http)) { uri = uri.Replace(http, string.Empty); } else if (uri.Contains(https)) { uri = uri.Replace(https, string.Empty); } return uri.Contains("//"); } 

See: https://pro-papers.com/

-2
Mar 20 '17 at 18:40
source share



All Articles