Try our conversational search powered by Generative AI!

Html.DisplayFor() not loading custme template on DXP environment



We are in process of upgrading EpiServer 11 to 12 (Optimizely DXP). For images we uses html helper method @Html.DisplayFor() and it works fine on my windows computer, but when the code is deployed to DXP it doesn't load custom template / custom template is not getting called.

I have a block containing this line in razor view 

                        @Html.DisplayFor(m => newsPage.Image, new {preset = "latest-news-block"})

I have also custom template under Views/Shared/DisplayTemplates named Image.cshtml ->

@using EPiServer.ServiceLocation
@model ContentReference

@if (!ContentReference.IsNullOrEmpty(Model))
    var urlresolver = ServiceLocator.Current.GetInstance<UrlResolver>();
    var url = urlresolver.GetUrl(Model, null, new VirtualPathArguments {ContextMode = ContextMode.Default});
    var preset = ViewData["preset"];
    if (preset != null)
        url += (url.Contains("?") ? "&" : "?") + "preset=" + preset;

    var viewDataCssClass = ViewData["CssClass"];
    var cssClass = "img-responsive";
    if (viewDataCssClass != null)
        cssClass += " " + viewDataCssClass;

    <img class="@cssClass" src="@url"/>

Screenshot from DevOps of the folder and custom template name

Edited, Mar 13, 2023 17:45


If it works locally (and not a build artifact) and not on the servers, it could be down to the file missing in the deployment package.

You could check the build logs for any signs. Or download the build artifact and check that the folder and file is included.

Mar 13, 2023 19:15

Thanks Stefan for your input.

I just downloaded the artifacts and both folder and file is there 

I have had issues with DXP environment before where index.cshtml doesn't contained capital I and because windows is case in-sensitive system, so it was working fine for me., whereas Linux (DXP) is case-sensitive file system.

As mentioned here

the folder name is case-senstive on case-sensitive file systems 

Mar 13, 2023 21:27

Any errors thrown in Logs? 

Have you tried moving the Image.cshtml up a directory (/Views/Shared) ? 

Mar 13, 2023 23:24

Views should generally not be in the wwwroot folder. Only static files that should be externally available should be there.

Sounds like those templates are considered content when publishing the project.

Mar 14, 2023 6:40

Views are not in wwwroot (static files). I have two wwwroot folders. 

See scrennshot ->

Mar 14, 2023 8:43

Actually, on my sites the view files are automatically precompiled into the DLL assembly of the project they are created in. So the Views folder is not even part of the deployment package.

Might be worth looking into your build pipeline, to see if the publish step is doing something wrong.

Mar 14, 2023 18:22

Thanks a lot for all the input. I have just changed DisplayFor() with 

@Html.PropertyFor(m => m.CurrentPage.CoverImage, "Image")

It was just not worth to keep fighting with DXP environment and their support. By explictly setting Template Name in PropertyFor() method, it works.

Mar 17, 2023 9:37
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.