5

Controller:

public ActionResult Tool(string id)
{
 // Code goes here . . 
}

View:

<a href="/Home/@item.Type/@Url.Encode(item.Title)" id="toolTitleLink">@item.Title</a>

From the code above @item.Title can have special characters like '/' sample link is http://localhost:39727/Home/Tool/C+Compiler+For+The+Pic10%2f12%2f16+Mcus when I try to navigate to that link Tool Controller was not called. I used @Url.Encode but still Controller was not called.

asked Jun 10, 2016 at 0:09
2
  • 1
    You can catch all symbols in id after /Home/Tool/ part with catchall route. See this answer Commented Jun 10, 2016 at 0:23
  • Thanks @IvanGritsenko It works but I want to apply it to multiple Action, do I need to create Custom Route for each action? Commented Jun 10, 2016 at 1:11

1 Answer 1

2

Unfortunately even if you use System.Uri.EscapeDataString instead of Url.Encode like so:

<a href="/Home/@item.Type/@System.Uri.EscapeDataString(item.Title)" id="toolTitleLink">@item.Title</a>

The rendered page will have the slashes encoded (look in the 'view source' of the page) the browser will still decode them.

You have two options, as far as I can see:

  1. Use a different character - make your own escape (so to speak ;)) - for example using tilda (~) or whatever else URL valid character you want, in order to replace the forward slash.

  2. Create a special route for the action with a catch-all at the end and parse things from the action.

answered Jul 6, 2016 at 13:46
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.