Alex
Alex

Reputation: 2959

How to protect slash in asp.net mvc and avoid "Failed to map the path" error?

I've got this razor html in a area in my ASP.NET MVC project:

<td>
    @Html.DisplayFor(x => x.AMOAResponsible, Model.AMOAResponsible ?? string.Empty)
</td>

Everything is ok when the value of AMOAResponsible is something like "Lorem Ipsum". But sometimes the value "AMOAResponsible" contains a slash (e. g. "LOREM/IPSUM") and then I get the following error:

Failed to map the path '/Areas/Dashboard/Views/Psr/DisplayTemplates/LOREM/IPSUM.aspx'.

I have no idea how to fix this. Can I protect the "/" ?

Upvotes: 2

Views: 514

Answers (1)

user3559349
user3559349

Reputation:

Your using this overload of @HtmlDisplayFor() where the second parameter is templateName and specifies the name of the DisplayTemplate to use to render the property

If the value of AMOAResponsible is (say) "Lorem", the method will search the /Views/yourControllerName/DisplayTemplates folder and search for a file named Lorem.cshtml or Lorem.aspx, which wont exist, so it then searches in /Views/Shared/DisplayTemplates which also wont exist, so it uses the default DisplayTemplate (no exception is thrown - but just wasted some processing time)

If however the value is Lorem/Ipsum it will start searching in /Views/yourControllerName/Lorem/DisplayTemplates but because the folder does not exist an exception is thrown.

I assume you are under the belief that Model.AMOAResponsible ?? string.Empty would render an 'empty string' if the value of Model.AMOAResponsible is null, but the helper does that anyway (i.e. generates no output) so just remove it. The helper should be just

@Html.DisplayFor(x => x.AMOAResponsible)

Upvotes: 3

Related Questions