MVC2: Need to route Urls containing # symbol - asp.net-mvc

I am looking for a way to correctly route urls that contain the '#' symbol. For such urls i basically want to ignore the #.
For example I want Stores/Index/#/{storeName} to route to the Index action of the Store controller passing a single parameter (storeName).
I have tried matching the literal '#' in the string but this is not working. (The action is called but the storeName parameter is not passed)
routes.MapRoute("RemoveHash", "Store/Index/#/{storeName}",
new {controller = "Store", action = "Index", storeName = UrlParameter.Optional});
I have also tried having 2 parameters to the action (the first being the #), thinking that I could just ignore the # if it was passed (hacky i know)... but something goes wrong with the routing in this case and neither parameter is passed to the action.
I would like to avoid using a HttpHandler for this task, if I could handle this using the MVC routing system that would be ideal.
Any suggestions?

What you are trying to do is impossible. Everything that follows the # (hash) sign in the URL is completely ignored when the browser sends a request to the server, so your ASP.NET MVC application could never get this value. Only client side javascript could read this value (using window.location.hash) and could pass it to the server using AJAX request and a normal URL (for example: Store/Index/{storeName}).

Related

What is the difference between Redirect and RedirectToAction in ASP.NET MVC?

What is the difference between Redirect and RedirectToAction other than their return type?
When do we use each? Explanation with any real life scenario would help me greatly.
I was looking at Confusion between Redirect and RedirectToAction, but, to me, it looks like the answer is more specific towards handling id parameter and returning proper view.
RedirectToAction lets you construct a redirect url to a specific action/controller in your application, that is, it'll use the route table to generate the correct URL.
Redirect requires that you provide a full URL to redirect to.
If you have an action Index on controller Home with parameter Id:
You can use RedirectToAction("Index", "Home", new { id = 5 }) which will generate the URL for you based on your route table.
You can use Redirect but must construct the URL yourself, so you pass Redirect("/Home/Index/5") or however your route table works.
You can't redirect to google.com (an external URL) using RedirectToAction, you must use Redirect.
RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table.
Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.
Best Practices: Use RedirectToAction for anything dealing with your application actions/controllers. If you use Redirect and provide the URL, you'll need to modify those URLs explicitly when your route table changes.

ASP.net MVC Route 404 with Encrypted text

I'm implementing a Password Reset facility in an asp.net MVC 3 web application. The email sent to the user contains a link with an encrypted string. Below is a sample link:
forgotprocess/QU1jfNoTb1Qd7qObop1FinQai4hCbzg7%2bMGfMF63d9Vvahi%2bmg9cT8KyaGo9jE1gbsWl5r%2f6DzpcRLf6HYNGeeFujG9QeblKUUvfxLDJ7UwcSCKD2AdsrR3EmC80PNCGGnGMQiya7ILNOJjWh%2fKSRQ%3d%3d
When the link is clicked I get a 404 error. To test the routes I used RouteDebugger, however I still get the 404 page - seems that the link isn't getting to the application. However if I change the link to the following:
forgotprocess/?i=QU1jfNoTb1Qd7qObop1FinQai4hCbzg7%2bMGfMF63d9Vvahi%2bmg9cT8KyaGo9jE1gbsWl5r%2f6DzpcRLf6HYNGeeFujG9QeblKUUvfxLDJ7UwcSCKD2AdsrR3EmC80PNCGGnGMQiya7ILNOJjWh%2fKSRQ%3d%3d
It works fine. I'd prefer not to have to use a query string parameter.
The size of the overall link is about 200 characters, so it shouldn't hit any limits?
Mark
In your top route:
forgotprocess/QU1jfNoTb1Qd7qObop1FinQai4hCbzg7%2bMGfMF63d9Vvahi%2bmg9cT8KyaGo9jE1gbsWl5r%2f6DzpcRLf6HYNGeeFujG9QeblKUUvfxLDJ7UwcSCKD2AdsrR3EmC80PNCGGnGMQiya7ILNOJjWh%2fKSRQ%3d%3d
%2f is going to get URL decoded into / so it's going to confuse the routing engine.
Can you alter how you're encrypted string is being generated to prevent this?
Alternatively, if it's the last parameter, you could alter your route like in this post, but that might lead to other issues:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{*id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" }); // Parameter defaults

MVC3 Razor URL.Action Parameter Value with forward slash. giving error

Its a small question on MVC3 Razor, I have a string ID like "a/b" when I am trying to call details or delete method of Controller. Getting error as system assuming "a/b" as 2 parameters but I have to pass this in one string value parameters.
--Edit
< a href="#Url.Action("Details", "Search", new {id = "a/b"})">Details </a>
My Controller/method is like
Search/Details (string id)
And I want to send id = 'a/b' . but .Net assuming it as 2 parameters in URL.
Please suggest.
It appears that forward slashes are not automatically encoded, and the reason is probably because even if they are encoded (%2f), by the time they reach the routing engine they have been decoded back to a forward slash. (Search for Robj in this post by Phil Haack (former manager on the MVC team)).
However, .NET MVC Routing w/ Url Encoding Problems poses the same problem, and it appears that the only way to solve this is to insert the encoded slash into a query string. Something like this:
< a href="#Url.Action("Details", "Search")?id=#Url.Encode("a/b")">Details </a>
and then, dealing with it in your method by accessing:
Request["id"]
You should use an escaped URL, so that parameter would be a%2Fb

how can i ignore the timestamp that jQuery adds to the ajax url in my routing?

Edit: Sorry guys, but I wasn't seeing this behavior when I came into work the next day and couldn't reproduce it. Something else must have been going on. I was going to delete the question but you can't do that anymore. Since there aren't any upvotes anywhere, no harm done.
I'm pulling data in to a div via a jQuery ajax call. Since I'm using IE9 primarily, I need to disable output caching in jQuery using cache: false, on the ajax call. That produces a URL that looks like:
http://localhost/site/UserDetails.mvc/48d76cdd-da6f-414d-ba63-f24708d351ff?_=1315347866786
What I actually want is:
http://localhost/site/UserDetails.mvc/48d76cdd-da6f-414d-ba63-f24708d351ff
Note the ?_=1315 toward the end of the first one. I'm pretty sure that's a timestamp that jQuery is adding to prevent output caching. This is breaking my mvc routing, which is expecting a single ID field at the end of the route:
routes.MapRoute(
"DefaultNoAction", // Route name
"{controller}.mvc/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
So I'm getting a 404 for the URL that ends with the timestamp. I'm pretty new to MVC and I don't know how to tell the router that any url parameter that is named _ should be ignored. How would I do this?
Take a look at the ASP.NET MVC: url routing vs querystring thread, where discussed how to handle this case.
This is breaking my mvc routing, which is expecting a single ID field at the end of the route
No, this is not breaking anything on your routes. Query string parameters are not part of the routes. They are ignored.

UrlEncode of link ids with ASP.NET MVC HtmlHelper extension methods

I have actions that take string id parameters that are based on a username which can include characters that require encoding, for instance "user?1"
If I use ActionLink() to generate the links, passing the string without encoding, it generates a link like this: http:\\localhost\controller\action\user?1, and the action gets passed "user" as the id.
If I UrlEncode() the string before passing it to ActionLink, then the link generated is: http:\\localhost\controller\action\user%253f1 as ActionLink will then encode the '%' character for you. Besides this looking ugly, it then also generates a HTTP Error 400 - Bad Request when following the link which I've not yet tracked down the cause of.
Is there any way that I can generate the url like: http:\\localhost\controller\action\user%3f1?
How about removing the ? character or replacing it with something else like a dash (-) or underscore (_) ?
You should look in the Global.asax.cs file
add another route for your convenience, in this case, the ff. might work:
routes.MapRoute(
null,
"{controller}/{action}/user/{id}",
new { controller = "Home", action = "Index" }
);
I guess this is what you want, to separate action for each users, but i suggest you use cookie for this purpose.
PS: Remember to put that one on top of your default route since routing is trying to match from top to bottom.
you can create a jquery plugin that check all the links and replace the char that you need to replace with the new value.
and after apply this plugin to all the ActionLinks

Resources