2

I have a very simple list of programming languages stored in a database, such as, C#, Java, F# etc. and these are listed in a razor view. To see the detail of it when then url like ...\language\java works but for ...\language\C# and ...\language\C# did not work.

The hash sign (#) is ignored and in Action the id parameter is "C" instead of "C#" and "F" instead of "F#".

Here is the code

@foreach (var item in Model.Languages)
{
    <li>
        <a href="/Language/@item.Title">@item.Title</a>
    </li>
}

When click on the link then it will call Detail Action in Language controller with parameter such as java, C# etc. But in action method I get id="C" instead if "C#"

Can you please help me how to handle this scenario.

8
  • how are you displaying the URL? From the model to direct HREF? or are you using @Html.ActionLink or Url.Action ? Commented Dec 11, 2017 at 2:46
  • can you post the razor code your currently have? Commented Dec 11, 2017 at 2:46
  • The # is a fragment identifier and the browser does not send anything in a url after the # symbol (consider using ../language/CSharp instead) Commented Dec 11, 2017 at 2:50
  • To see the detail of it when then url like ...\language\java works but for ...\language\C# and ...\language\C# did not work. Please show us that code. Commented Dec 11, 2017 at 2:53
  • 1
    Possible duplicate of C# method to do URL encoding? Commented Dec 11, 2017 at 2:54

3 Answers 3

1

You need to url encode some chaacters (such as #), you need to pass %23 for #

Check here all encodings https://www.w3schools.com/tags/ref_urlencode.asp

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

Comments

1

Use HtmlHelper or UrlHelper to generate action urls, they will help to encode the characters when needed

@foreach (var item in Model.Languages)
{
    <li>
        @Html.ActionLink(item, "Detail", "Language", new { id = item }, null)
    </li>
}

3 Comments

That will not work! Anything after the # is not sent to the server
@StephenMuecke The # will be encoded into %23 in the query string, e.g. language?name=C%23 If there are other parameters, the query string will be like language?name=C%23&p1=111&p2=222...
Yes, but OP is generating ../language/C# in the address bar. You cannot assume that a user will only ever navigate by clicking a link
0

It doesn't sent to server . Use CSharp or FSharp instead of C# or F#

Comments

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.