0

I had a problem sending a string back as a routeValues parameter from an MVC ActionLink if I have BOTH of the conditions below:

  1. The parameter name is "id"
  2. The data contains a full stop, e.g. mystring.bad

I have see this question and this question which refer to a problem with using "id" but they don't seem to cover the case above.

Below I have listed different ActionLinks with the urls they produce listed below each one: the first two work, the last two fail.

@*Links that work*@
@Html.ActionLink("Test OK", "About", new { id = "mystring" }, new { @class = "k-button" })
@*Above produces url http://localhost:55745/Home/About/mystring*@
@Html.ActionLink("Test OK", "About", new { stringId = "mystring.bad" }, new { @class = "k-button" })
@*Above produces url http://localhost:55745/Home/About/mystring?stringId=mystring.bad*@

@*Links that FAIL*@
@Html.ActionLink("FAIL: 4 param ActionLink", "About", new { id = "mystring.bad" }, new { @class = "k-button" })
@*Above produces url http://localhost:55745/Home/About/mystring.bad*@
@Html.ActionLink("FAIL: 5 param ActionLink", "About", "Home", new { id = "mystring.bad" }, new { @class = "k-button" })
@*Above produces url http://localhost:55745/Home/About/mystring.bad*@

The last two that fail produce the following error: "HTTP Error 404.0 - Not Found".

Anyone got an idea on how to fix this? I can of course change the parameter name to something other than "id" but I would like to know why this does not work.

Notes:

  • The three parameter, i.e. without the HtmlAttributes, also fails in the same way.
  • My route table is just the default MVC setup.
Community
  • 1
  • 1
Jon P Smith
  • 2,391
  • 1
  • 30
  • 55

1 Answers1

1

As best I can tell, the latter two URLs fail because ASP.NET thinks that anything after the dot is a file extension, so it hands off to the file handler, which can't find the file, hence the 404.

(The second URL succeeds because the above restriction doesn't apply to QueryString values).

What's really interesting is that if you add a "/" to the end of the URL, it works just fine.

Supposedly setting a property<httpRuntime relaxedUrlToFileSystemMapping="true"/> in your web.config works for .NET 4.0, but I couldn't get this to work on my setup (.NET 4.5 and MVC5).

If those don't work, you can take a look at this answer.

Community
  • 1
  • 1
Matthew Jones
  • 25,644
  • 17
  • 102
  • 155
  • Hi @Matthew Jones. Thanks for a a detailed reply, and especially for the link to the other answer which explains it really well. Having seen that answer I'm rather surprised that the second ActionLink, i.e. the one with `new { stringId = "mystring.bad" }`, produces the URL it does. I think that version is going to be my final solution, i.e. make sure the name of the parameter is not "id". Anything else is messy. – Jon P Smith Jan 16 '15 at 19:43