Question

This is my html.ActionLink:

 @Html.ActionLink("Comment", "Comment", new { id = item.NewsId, title = item.Title + "#disqus_thread" })

it gives me following url:

http://localhost:62394/Home/Comment/5008/Iran%20women%20barred%20from%20presidency%23disqus_thread

instead of "#" it generates "%23"

How can I make sure it becomes like this:

`

http://localhost:62394/Home/Comment/5008/Iran%20women%20barred%20from%20presidency#disqus_thread

Any kind of help is appreciate alot!

Was it helpful?

Solution 2

Try this:-

Convert ActionLink to string use URLDecode and change it to render it as Html.Raw.

@Html.Raw(HttpUtility.UrlDecode(Html.ActionLink("Comment", "Comment"
, new { id =  
 item.NewsId, title = item.Title + "#disqus_thread" }).ToString()))

OTHER TIPS

You should use Url.Action like this:

<a href="@Url.Action("Comment", new { id = item.NewsId, title = item.Title })#disqus_thread">Comment</a>

I believe it's simpler, cleaner and safer.

when we pass special characters in query string of the requested url, it may cause error unless they are properly encoded. These special characters can be handled by using the Hexadecimal Value of the characters instead of the characters themselves.

so instead of using #

@Html.ActionLink("Comment", "Comment", new { id = item.NewsId, title = item.Title + "#disqus_thread" })

use Hexadecimal Value %23

@Html.ActionLink("Comment", "Comment", new { id = item.NewsId, title = item.Title + "%23disqus_thread" })

i hope this may help you..

I was having the same issue but it was in a DevExpress Grid Link. This was the only way I was able to resolve it.

@Html.ActionLink("Comment", "Comment", new { id = item.NewsId, title = item.Title + "#disqus_thread" }).ToString().Replace("%23", "#")

Let's say you have to create URL that point to Bootsrap 4 Tab that has two tabs only.

@Html.Raw(HttpUtility.UrlDecode(@Html.ActionLink("My second TAB", "Index#second", "Home", null, new { @class = "btn-link" }).ToString())) 

So final URL will look likes

http://localhost:62959/Home/Index#second

You should decode the URL before inserting it:

@HttpUtility.UrlDecode(Html.ActionLink("Comment", "Comment", new { id = item.NewsId, title = item.Title + "#disqus_thread" }))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top