Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

FindView fails to find view when using pre-compiled views #6676

Closed
Dav-id opened this issue Aug 17, 2017 · 4 comments
Closed

FindView fails to find view when using pre-compiled views #6676

Dav-id opened this issue Aug 17, 2017 · 4 comments
Assignees

Comments

@Dav-id
Copy link

Dav-id commented Aug 17, 2017

Issue

Since updating to 2.0 I have noticed my DisplayTemplates that are loaded into a String via FindView are no longer able to find the View when in production where the pre-compiled views has been defaulted to on.

Details

The pre-compiled view option enabled by default does not include the source views when publishing (obviously) so it appears the IRazorViewEngine.FindView is unable to locate the view in the DLL.

This may not be unique to 2.0 but more to do with the default pre-compiled views being switched on and until now not something I had enabled manually.

        var engine = _serviceProvider.GetService(typeof(IRazorViewEngine)) as IRazorViewEngine;
        var tempDataProvider = _serviceProvider.GetService(typeof(ITempDataProvider)) as ITempDataProvider;

        if (engine == null) throw new Exception("Can't find IRazorViewEngine");

        var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider };
        var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());


        var viewEngineResult = engine.FindView(actionContext, name, false);

        if (!viewEngineResult.Success)
        {
            throw new InvalidOperationException(string.Format("Couldn't find view '{0}'", name));
        }

Expected Behaviour

I would expect the ViewEngineResult FindView(ActionContext context, string viewName, bool isMainPage) method to be able to locate the view in the pre-compiled views DLL and output in the same way as if it was not pre-compiled.

Thanks,
David

@pranavkm
Copy link
Contributor

Your expectations are correct, the view engine should discover the precompiled view. What's the value you're attempting to pass in to FindView. Your might be running in to #6672.

@Dav-id
Copy link
Author

Dav-id commented Aug 17, 2017

The view name I am passing in is Shared/DisplayTemplates/ContentCard which is located in /Views/Shared/DisplayTemplates/ContentCard.cshtml

I have tried using paths like /Views/Shared/DisplayTemplates/ContentCard.cshtml, /Views/Shared/DisplayTemplates/ContentCard and ~/Views/Shared/DisplayTemplates/ContentCard.cshtml and other permutations of this that I could think of without luck.

Simply disabling pre-compiling the views on deployment resolves the issue but we obviously lose the benefit of that process.

@Eilon Eilon added this to the 2.1.0 milestone Aug 17, 2017
@pranavkm
Copy link
Contributor

@Dav-id either ~/Views/Shared/DisplayTemplates/ContentCard.cshtml or /Views/Shared/DisplayTemplates/ContentCard.cshtml would be the right values to use when looking things up as paths. That said, the view engine has two overloads, one meant for paths and one for "names". You would need to use the latter to make things work. See https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/ViewResultExecutor.cs#L79-L84 for how ViewResult use these with fallback to discover either.

@pranavkm
Copy link
Contributor

pranavkm commented Sep 5, 2017

Closing this in favor of #6672 since it has more details.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants