简体   繁体   中英

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 (eg "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 "/" ?

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 Model.AMOAResponsible ?? string.Empty would render an 'empty string' if the value of Model.AMOAResponsible is null , but the helper does that anyway (ie generates no output) so just remove it. The helper should be just

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM