How to access AJAX hash values in ASP.NET MVC?

2019-02-25 04:27发布

问题:

I'm considering using the hash method to create static urls to content that is managed by ajax calls in a Asp.Net MVC. The proof of concept i'm working on is a profile page /user/profile where one can browse and edit different sections. You could always ask for the following url /user/profile#password to access directly to you profile page, in the change password section

However, i'm wondering if i'm not starting this the bad way, since apparently i can't access the part after the hash in any way, except by declaring a route value for the hash in global.asax. So i'm wondering if this is the right way to access this part of the url?

Am i supposed to declare a route value, or is there another way to work with hash values (a framework, javascript or mvc)?

Edited to add: In pure javascript, i have no problem using the window.location.hash property, i'm not sure though how standard it is in today's browsers, hence the question about a javascript framework/plugin that would use it.

回答1:

The thing is that the part that follows the hash (#) is never sent to the server into the HTTP request so the server has absolutely no way of reading it. So no need to waste time in searching for something that doesn't exist.

You could on the other hand tune your routes to generate links that contain the hash part so that client scripts can read it.



回答2:

Send the hash value document.location.hash as a parameter to the controller action of your choice.



回答3:

This can be done in the code if needed...

RedirectResult(Url.Action("profile") + "#password");

should work fine