views:

29

answers:

1

The problem with ASP.NET 4.0 routing is that the Page.RouteData.Values does not contain the paramenters after # character from the link

System.Web.Routing.RouteTable.Routes.MapPageRoute("ProjectViewRoute1", 
"project/{title}/{idProject}#{idDesign}", "~/ProjectView.aspx");

As I said, the Page.RouteData.Values.ContainsKey("idDesign") will return false

The reason I want to make use of this feature is because I use JavaScript and Ajax to hide some content and load new one, wich in eyes of an user is like loading a different page, and he must be able to copy paste the URL and view that page later.

The question is: how to get the {idDesign} from the RoutedData ?

+2  A: 

Browsers don't send data after the # in the URLs to the server; as a result, it is not possible for ASP.Net to capture that data and provide it to you.

I would recommend using a ? instead of your # to get the functionality you need, and include an AJAX call to capture data placed in the hash section of the url to send to the server, if necessary, for AJAX-created urls.

Using jQuery:

$(function () {
    if (location.hash) {
        hash = location.hash.substr(1);
        location.hash = null;
        location.search = hash;
    }
});
mattdekrey
If I'll use the ? character my page will be refreshed and will reload from server. When I set the URL from JavaScript like this: window.location = http://example.com//page/subpage#idDesign my page will not be refreshed and I can easily load the new page using ajax. The problem is to get back on the server the string after # character. I know this can be done someway. Facebook does it, ASP.NET with ASP.NET Ajax and seo urls also does it.
pixel3cs
Right, Facebook does it by having a quick URL-rewrite that replaces everything after the ? with everything after the # - which is what I recommended in the second part of my answer. I can provide source if you require.
mattdekrey
I went ahead and provided source for you, since I had it on another project.
mattdekrey