ASP.NET MVC Route parameter separate by ":" char - asp.net-mvc-2

Now I am not sure it practical or not but I like to have a URL like this
http://example.com/field1_query:field2_query map but it seem like ASP.NET MVC Routing not happy with the ":" char here my route
routes.MapRoute("filter",
"{field1_query}:{field2_query}",
new { controller ="...", action="..."} );
It doesn't work but if I change to {field1_query}/{field2_query} it works fine (which mean my URL also change http://example.com/field1_query/field2_query).

The colon is a reserved character in a URL. See the IETF spec. You would need to escape any restricted or reserved characters.

Related

How to use : colon char in Bottle route

I have simple Bottle route, and URL should contain : (colon) sign. URL should be like /REST/item:128 or /REST/item:89753
My route is
#route('/REST/item:<id:int>')
def icc(id):
return { 'id': id }
Route is not working properly. id contain only last char from url id, not full id.
How to use : (colon) in route?
Wow, this was confusing.
I haven't had time to fully understand what's happening, but I suspect that one of Bottle's route regexes is eating too many characters when there's a colon in the route.
In any case, escaping the colon with a backslash appears to solve the problem:
#route(r'/REST/item\:<id_:int>') # note the "r" prefix there
def icc(id_):
return {'id': id_}
Here's a test request and its response:
=> curl -v 'http://127.0.0.1:8080/REST/item:123'
{"id": 123}
EDIT: Mystery solved.
Bottle currently supports two syntaxes for url wildcards: The
one (since 0.10) and the :old syntax. Both are described here:
http://bottlepy.org/docs/dev/routing.html
In your example, the : triggered the old syntax. The solution is to
escape the colon with a backslash (as described in the SO answer).
Escaping is fully implemented and works as intended, but is
undocumented. This is why I leave this issue open. Pull requests for
better documentation would be welcomed.

MVC2 Slash char in url

I want to use encrypted strings in MVC2 urls. A typical url in my app looks like this:
http://localhost:29558/Account/PasswordReset/ZKGeDMZikfIsnO8/MEs7SCBlI+MZo1Je8LM5dTEeCt3u91ARPUcavT5UXfVVRfyE
Note that everything after PasswordReset/ is the encrypted string. In the example the encrypted string contains a slash, and this is causing MVC to crash.
I've tried adding a MapRoute in Global.asax.cs as follows:
routes.MapRoute(
"PasswordResetSpecialCase", // Route name
"Account/PasswordReset/*", // URL with parameters
new { controller = "Account", action = "PasswordReset" } // Parameter defaults
);
but MVC2 is still falling over because the encrypted string contains a slash char. If I remove the slash then it works, but obviously that's no good.
How do I get MVC2 to regard everything after the PasswordReset as pure data?
Thanks.
Your maproute contains an error. Replace the * with {*nameOfParameter}

ASP.NET MVC 2 wildcard route has trouble handling spaces

I've got a wildcard route mapped as below:
routes.MapRoute(
null,
"{controller}/{action}/{*category}",
new { controller = "Mall", action = "Index", category = UrlParameter.Optional }
);
This has been working fine until the category has any spaces before or after slashes " / ".
For the category ART/MUSIC, it will find the page fine.
For the category ART / MUSIC, it will give me a 404 not found.
Any help would be much appreciated!
I've just answered kind of the same question here. For completeness:
If an empty space is at the end of any section of the URL before the next slash, it throws a HttpException in the System.Web.Util.FileUtil.CheckSuspiciousPhysicalPath() method which is handled by MVC and you'll get a HTTP 404 response.
You can verify that yourself by checking the checkbox for Throw in:
Visual Studio
Debug
Exceptions
Common Language Runtime Exceptions
Generally you should not have empty spaces in your URLs. I personally format my urls, that all spaces becomes a dash (-).

How to route lower-case URLs ('questions/add_to_favorites/123') with underscores in ASP.NET MVC2?

ASP.NET MVC 2 controllers and actions use UpperCamelCase.
For some reasons many big sites, including SO, use lowercase (with underscore) for controllers and actions in the urls. Examples:
https://stackoverflow.com/questions
https://stackoverflow.com/users/377920/randomguy
http://www.reddit.com/ad_inq/
http://www.wired.com/special_multimedia/mobile/
etc.
I would like to know how this is accomplished.
The default router seems to be case-insensitive, ie. stackoverflow.com/questions/ask will be directed to Questions-controller's Ask() method without a problem.
However, say we want to direct questions/add_to_favorites to Questions-controller's AddToFavorites() action.
How is this accomplished?
Is it now required to use Html.ActionLink("add_to_favorites")
instead of Html.ActionLink("AddToFavorites") to make the links in the HTML point as questions/add_to_favorites instead of Questions/AddToFavorites?
Edit:
Similar posts
How can I have lowercase routes in ASP.NET MVC?
ASP.NET MVC: Get lowercase links (instead of Camel Case)
One way to support underscores is to use the ActionName attribute:
[ActionName("add_to_favorites")]
public ActionResult AddToFavorites() {
// ...
}
However, this doesn't work for controllers. Perhaps if we could somehow remove all the underscores from the request before it gets to the routing mechanism, then it would work.
You can add custom routes manually. This is not an universal solution and must be added for every controller and action separately.
routes.MapRoute(
"Web2.0 RoR style lowercase URLs with underscores",
"questions-foo/add_to_favorites",
new { controller = "Questions", action = "AddToFavorites" }
);
The cool thing is that the URL generating Html-helper methods don't need to be modified. The routing table is used to route incoming requests and to generate URLs. So,
Html.ActionLink("Add to favorites", "Questions", "AddToFavorites"); maps to /questions-foo/add_to_favorites.
Note that the original /Question/AddToFavorites still works as does /qUeStIoN/aDdtOfAvOrItEs as well as /qUeStIoNs-FOO/ADD_TO_FAVORITES because the default routing mechanism is case-insensitive.

Zend Framework, mapping the extension of a URL to the format parameter?

It is possible to map the extension of a URL, to the format parameter in ZF?
I'd like the default routing to still work, including mapping parameters from the URI, so you'd be able to say:
http://example.com/controller/action/param1/value1/param2/value2.json
Here: $this->_getParam('format') => "json"
And likewise:
http://example.com/module/controller/action/param1/value1/param2/value2.xml
Here: $this->_getParam('format') => "xml"
I've fiddled with the default routes, but i cannot get it to work..
You could create a regex route, ending with something like (\w+)(.(\w+))? and capture the part after the . as the .3 capture. see Zend_Controller_Router_Route_Regex