Razor engine cant find view

前端 未结 2 1985
暖寄归人
暖寄归人 2021-01-12 04:28

Im trying to render a html from a view without using a web request. I need the HTML as a string, internally, i do not wish to serve it.

The viewEngine.FindView() re

相关标签:
2条回答
  • 2021-01-12 05:02

    I had the same issue. I found the answer here: GitHub aspnet/Mvc Issue #4936

    Basically, use GetView instead of FindView, like this:

    var viewResult = razorViewEngine.GetView(viewName, viewName, false);
    
    0 讨论(0)
  • 2021-01-12 05:02

    We have a helper method defined to render optional views which may or may not exist:

    public static Task RenderPartialAsyncIfExists(this IHtmlHelper htmlHelper, ICompositeViewEngine engine, string partialViewName, object model)
    {
        if (engine.GetView(partialViewName, partialViewName, false).Success)
        {
            return htmlHelper.RenderPartialAsync(partialViewName, model);
        }
    
        return Task.CompletedTask;
    }
    

    It's used on view pages like:

    @inject ICompositeViewEngine Engine
    ...
    @{ await Html.RenderPartialAsyncIfExists(Engine, $"~/Views/Shared/_navigationAdmin.cshtml"); }
    

    This works find locally (IIS Express) but for some reason was failing when deployed to IIS.

    In my case, there was something wrong with the .csproj file, where the view in question was removed but then re-added as an embedded resource:

        <ItemGroup>
            <Content Remove="Views\Shared\_navigationAdmin.cshtml" />
        </ItemGroup>
        
        <ItemGroup>
            <EmbeddedResource Include="Views\Shared\_navigationAdmin.cshtml" />
        </ItemGroup>
    

    Removing those two sections from the .csproj fixed the problem in IIS.

    This is using (EOL) AspNet Core 2.2

    0 讨论(0)
提交回复
热议问题