2

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!

1
  • How can i do same thing in MVC controller class when i am passion RedirectToAction Commented Nov 12, 2013 at 15:07

6 Answers 6

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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()))

Comments

0

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..

Comments

0

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", "#")

Comments

0

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

Comments

-1

You should decode the URL before inserting it:

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

1 Comment

Hmm its not working I am recieving 2 Errors The best overloaded method match for 'System.Web.HttpUtility.UrlDecode(string)' has some invalid arguments. Argument 1: cannot convert from 'System.Web.Mvc.MvcHtmlString' to 'string'

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.