Asp.net mvc3 Hash(#) handing in routing system - asp.net-mvc

I use location.hash javascript mathod a lot at my ajax requests. Generally I replace standard
{action}/{id} with {action}#{id}
How can I introduce this strategy at rounting system?
I wol like write:
#Html.ActionLink(text, action, controller, new { id }, new { })
and this generates /controller/action#id ?
Oh may be it's not good idea?
How do you process ajax requests(open some entity at list?)
Thanks

The answer you seek is here:
How to access AJAX hash values in ASP.NET MVC?
TL;DR - Its impossible.

Remember: Routing isn't just for generating links; it's also for parsing incoming URIs. Incoming URIs don't have hash/fragments on them, so the route you propose, if it were possible, wouldn't work for incoming requests.
You can write your own HTML helper, in lieu of Html.ActionLink to create the URI with the hash, if you want, but it can't be a route.

this should work as you want.
RedirectResult(Url.Action("{action}") + "#{id}");

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.

How do I substitute dynamic parameter values to T4MVC using razor?

Consider following:
$("#myform").attr({ action: "#Url.Action(MVC.Thing.Delete().AddRouteValue("id", myJsModel.Id )) });
I'm trying to set the action method of the form to a strongly typed T4MVC route. How do I insert a dynamic value from javascript into the route value?
I've seen the use of #: but I don't know how to insert it back into razor.
I don't think that using T4MVC here versus regular MVC syntax makes much difference when it comes to this issue.
When thing you might try is to generate a replaceable token on the server and do the replacement client side. e.g. something like
MVC.Thing.Delete().AddRouteValue("id", "SOMETOKEN")
And then take the generated path on the client and replace "SOMETOKEN" with myJsModel.Id.

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.

How does ASP.NET MVC 2 handle different request formats (i.e. HTML, XML, JSON, JS, etc.)

I'm diving into ASP.NET MVC 2 and I'm trying to understand how it handles different request formats. In Ruby on Rails, you specify in the controller which response format to return based on the request...
respond_to do |format|
format.html #action.html.erb
format.xml { render :xml => #employees.to_xml(:root => "employees") }
In ASP.NET MVC 2...
How do you specify the request format?
How do you respond to that request with the requested format?
Thanks so much in advance! I apologize if this question is strange, I'm very new to the .NET world.
As others have said there is no built-in support for formats in ASP.NET MVC. I've seen people add a QueryString parameters "format" to indicate the kind of format required (XML vs HTML) but you still need to manually code the response type on your controller.
There is support for JSON in MVC, but again, you need to manually evaluate whether the request wants JSON provide it. In the case of JSON is typical to see something like this:
if (Request.IsAjaxRequest())
{
return Json(viewModel, JsonRequestBehavior.AllowGet);
}
Haven't seen anything like that in asp.mvc. As far as I was concerned I was filtering my requests based on [Post] or [Get] attributes.
The only thing I know is that you can allow Json requests to be processed by setting JsonRequestBehavior to JsonRequestBehavior.AllowGet option.
Download MVCContrib and here you go. You can also take a look at Simply Restful Routing.

ASP.NET MVC: How to allow some HTML mark-up in Html Encoded content?

Is there some magic existing code in MVC 2 to Html.Encode() strings and allow certain html markup, like paragraph marks and breaks? (coming from a Linq to SQL database field)
A horrible code example to achieve the effect:
Html.Encode(Model.fieldName).Replace("<br />", "<br />")
What would be really nice is to overload something and pass to it an array (or object) full of allowed html tags.
It's not a good idea to create your own whitelist based on regular expressions because you'll likely inadvertently open a security hole for XSS.
From Sanderson's book "Pro ASP.NET MVC3 Framework": "...The only viable mitigation is strict, whitelist-based filtering: use a library like the HTML Agility Pack to ensure the user-supplied markup contains only the tags that you explicitly allow."
Sanderson goes on to supply a link to a site that demonstrates a broad range of XSS techniques that you'd have to test for if you use the regex approach. Check out http://ha.ckers.org/xss.html
There is nothing built in to ASP.NET or MVC for this, but it's not that hard to write your own whitelist-based one with regular expressions and so on. Here's one that Jeff wrote, though it's pretty rough around the edges...
I can't think of anything off the bat but I guess you could write an extension method that allows you to add a paremeter/list of items to allow.
Html.Encode(Mode.fieldName, List<items> Myitems);
It could modify the allowable tags into < etc and then encodes the rest like normal.

Resources