Try our conversational search powered by Generative AI!

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

ZZ
ZZ
Vote:
 

Hi,

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

#298242
Edited, Mar 13, 2023 17:45
Vote:
 

Hi ZZ

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.

#298243
Mar 13, 2023 19:15
ZZ
Vote:
 

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 https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.rendering.ihtmlhelper-1.displayfor?view=aspnetcore-7.0#remarks

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

#298250
Mar 13, 2023 21:27
Vote:
 

Any errors thrown in Logs? 

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

#298285
Mar 13, 2023 23:24
Vote:
 

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.

#298286
Mar 14, 2023 6:40
ZZ
Vote:
 

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

See scrennshot ->

#298289
Mar 14, 2023 8:43
Vote:
 

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.

#298314
Mar 14, 2023 18:22
ZZ
Vote:
 

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.

#298484
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.