59

I need to implement hash value i.e the Url should look like this:

/home/index/#create

For this have added a route:

routes.MapRoute(
    "Default",    // Route name
    "{controller}/{action}/#{detail}",    // URL with parameters
    new { controller = "Login", action = "LogIn",  detail  =""}  // Parameter defaults
);

On accessing /home/index/#create, it is redirecting me to the default route.

How can this be done?

1
  • 2
    Based on some answers. I know with #hash, browser does not sending a request to the server BUT that only when the URL change for the second time within the same page. So this question still make sense. Think if user bookmark this URL! the #hash may refer to comment anchor or represent a selected menu. I still looking a right way to do this in route. Commented Jun 3, 2011 at 2:18

4 Answers 4

121

As stated there is no way to do this using routing. The only possible solution is to append the # fragment to your url when redirecting in the actions of your controller. Eg.

return Redirect(Url.Action("Index", "Home") + "#create");
Sign up to request clarification or add additional context in comments.

3 Comments

IMO this should be the accepted answer. The OP asked, "How can this be done?" and this is the only answer that gives you a reasonable workaround, rather than why you can't do it the way the OP originally wanted to.
I just spent 20 minutes trying to figure out how to make a "return View()" method to allow a hash parameter, or any other action result - this is perfect. Good answer!
Thank you for this answer! I recently had to implement the same thing, and this worked for me. However, I ran it in the HTML which required a slightly different syntax: href="@Url.Action("Index","Home")#deep-link"
54

You cannot fetch the value after the # symbol on the server simply because this value is never sent to the server. Only client side javascript has access to this so defining routes with hash doesn't make much sense.

3 Comments

It make sense. My #hash used to memorize selected menu (I don't want cookie). When user click a menu, the # changed and AJAX request to the server to update the respective content. By this way user able to bookmark the page with selected menu AND can use browser back button with readable #menuname. Next time when user browse with the #, then the loaded page is already have the selected menu without unnecessary AJAX request. I need to bind the hash value into route data because each # can be considered a different page.
You could use javascript to send this to the server as part of a form request.
NOTE: some APIs like Google's identity service use the value in the #hash to hold the token for exchange in the next call. So it would be nice to have an answer that addresses that.
7

When a browser makes a request for a URL, it does not send anything after a hash to the server. This route may enable you to generate route URLs containing the hash value but there is no way to do anything server-side when the user navigates to such a URL. That's just the way the web works...

Comments

0

OAuth 2.0 Implicit Flow

If this has to do with OAuth 2.0 Implicit Flow, you should use Authorization Code Grant instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.