Search code examples
javascriptasp.netrazorencodeuricomponent

Encode special characters in cshtml


I have a razor view which has a hidden field Model.Token. The Token consist of special characters. This Token is appended to a link in href.

<a href='http://[email protected]'>Link</a>

If Model.Token has a +, the link renders it as a blank space. That is, if

Model.Token = 'ABC+DE'

The link becomes

http://localhost.com?token=ABC DE

How do I encode the Token in the link so that + is not removed? Do I need to use have a js and use encodeURI, or is there a better way to achieve this?


Solution

  • You can try the System.Uri

    <a href='http://[email protected](Model.Token)'>Link</a>