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
Related
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
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.
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}).
I'm trying to make my URL look like this:
http://domain.com/controller/action/123/SomeTextForSEO
I tried using an Action Link, but that appends ?company=SomeTextForSEO instead of the company name after a slash.
<%: Html.ActionLink("DomainList", "Index", "DomainList", new { id = item.CompanyID, company = item.CompanyDisplayName.Trim() }, new object { })%>
and now I think I need to use a RouteValueDictionary but I'm unsure of how to do this in ASP.NET syntax. Can someone point me in the right direction? The MSDN examples are insufficient.
If you go to global.asax.cs and add a new route similar to the existing default route with the pattern
"{controller}/{action}/{id}/{company}"
Before the default one, you should find that the link will generate correctly with your ActionLink call as-is.
I'm on a phone so I'd like to be more verbose (a route restriction would be a good idea to prevent this route interfering with the default), but the HTC keyboard is not code friendly ;)
I'm doing some changes on my routes, and suddenly the following is appearing in my url's as a querystring:
?RouteValueDictionary=System.Web.Routing.RouteValueDictionary
So, my url's now look like
http://localhost:20367/Search/AdvancedSearchResults?RouteValueDictionary=System.Web.Routing.RouteValueDictionary
How do I make it disappear?
I know the routing puts it in there, because it cannot find route values for the viewmodel that's passed along, but I can't seem to fix it...
I suspect your call to Html.ActionLink is picking the wrong overload. You might need to add an extra parameter at the end to force .NET to pick the right overload:
Html.ActionLink("click here, "SomeAction", "SomeController", routeValues, null)