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?

p.campbell
  • 98,673
  • 67
  • 256
  • 322
facebook
  • 1,894
  • 3
  • 21
  • 28
  • 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. – CallMeLaNN Jun 03 '11 at 02:18

4 Answers4

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");
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 12
    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. – Shaul Behr Feb 17 '14 at 11:19
  • 2
    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! – Shawn J. Molloy Aug 11 '14 at 03:55
  • 1
    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"` – NominalAeon Jul 25 '20 at 15:20
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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 12
    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. – CallMeLaNN Jun 03 '11 at 02:37
  • 1
    You could use javascript to send this to the server as part of a form request. – Will Shaver Oct 24 '11 at 17:22
  • 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. – Berin Loritsch Apr 25 '18 at 23:30
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...

Robert Levy
  • 28,747
  • 6
  • 62
  • 94
0

OAuth 2.0 Implicit Flow

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

Joel Wiklund
  • 1,697
  • 2
  • 18
  • 24