soundaranbu / Razor.Templating.Core

Razor Templating Engine to render Razor Views(.cshtml files) to String in Console, Web, Service, Desktop workloads in .NET Core 3+

Home Page:https://soundaranbu.medium.com/render-razor-view-cshtml-to-string-in-net-core-7d125f32c79

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Layout page gets loaded with the partial view

flgatormike opened this issue · comments

When I calll RazorTemplateEngine.RenderAsync, the string that is returned includes my layout page. I'm wondering if this is by design, and if so, how can I specify a different layout page to use?

Hi @flgatormike , the razor rendering works the same way as the ASP.NET Core MVC.
In your case, if you have ~/Views/_ViewStart.cshtml in your project like below

@{
    Layout = "_Layout";
}

then, the partial view or view will inherit this layout file when you render it directly.

In order to overcome this, either remove the ~/Views/_ViewStart.cshtml file or add the following in your partial view or view file

@{ 
    Layout = null;
}

some helpful links: https://stackoverflow.com/questions/6613626/razor-view-without-layout

I hope the above information is helpful. Closing the issue. Feel free to re-open if required.