Jon P Smith
Jon P Smith

Reputation: 2439

html “id” in actionLink razor helper where string is of a "bad format"

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:

Upvotes: 0

Views: 310

Answers (1)

Matthew Jones
Matthew Jones

Reputation: 26190

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.

Upvotes: 1

Related Questions