Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass special character via url in ASP.NET MVC?

I want to have a url link like: /Posts/Post/1#comments

Where: Posts - controller name, Post - action name, 1 - id parameter

I am using following code in my View:

<a href="@Url.Action("Post", "Posts", new { id = @item.PostId + "#comments" })">Comments</a>

As a result I have: /Posts/Post/1%23comments

What to do to pass '#' char instead of "%23"?

like image 929
angelPL Avatar asked Jan 18 '26 03:01

angelPL


1 Answers

you're looking for:

<a href="@Url.Action("Post", "Posts", new { id = item.PostId })#comments" ...></a>

# is for hash, so to send it server-side (which Url.Action is expecting) means encoding it. If you're looking to supplement the client experience, don't include it in your Url.Action (or create a special overload that accepts a fragment identifier and outputs it un-touched).

like image 117
Brad Christie Avatar answered Jan 19 '26 16:01

Brad Christie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!