Is it possible to insert the character '#' (hash) in the MVC routes?

Asked

Viewed 63 times

2

It is possible to insert the character # (hash) on MVC routes? I am developing an MVC application in which one of the specifications is the use of # on the MVC routes.

Is it possible to do this natively? Is there any way around the situation?

Grateful!

  • Do you want to use as a normal character? That is, do you want to escape the character and not be interpreted by the browser? Tried to use %23?

  • Like a normal character. Like a SPA (angular) route. Example: http://mydomain.com.br/#/Cars? id=10.

  • You tried what I said?

  • Didn’t work out...

2 answers

3

Is it possible to do this natively? Is there any way around the situation?

Not. # is a symbol reserved for browsers to indicate anchors and cannot be part of routes.

As only the customer has access to what comes after #, it doesn’t make much sense to define routes with it. It would only confuse the browser and Javascript wouldn’t work right.

2


As already said, with routes it is not possible to do this.

One way to do this would be to alter the return of Controller for a Redirect() to the desired page.

Something like that:

 return Redirect("/Home/Contact#contato");

Or this:

return Redirect(Url.Action("Contact", "Home") + "#contato");

Remembering that this will only put "an anchor" at the end of the URL. I’m not sure what you want to do with it.

If you want more details, this question has more answers about what you asked.

Note: If you choose to use Redirect(), take care. If the request for the same Action(), will cause a Infinite Loop.

In case you want to do this on the client, you can customize the Htmlhelper and create your own, with the desired route.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.