Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncompiled partial view doesn't inherit from ViewImports

I moved a part of a view into a partial view.


_ViewImports.cshtml

@using AsonCore.Helpers
@using AsonCore.Models
@namespace AsonCore.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Application.cshtml

@page
@model ApplicationModel

<partial name="/Pages/Partial/_ApplicationPartial.cshtml" />

_ApplicationPartial.cshtml

@model ApplicationModel
<section class="content application">
    <div>
        <form method="post" enctype="multipart/form-data">
            <div>
                <label asp-for='email.Firstname'>FORNAVN</label>
                <input asp-for='email.Firstname' required />
            </div>
            <div>
                <label asp-for="email.Lastname">ETTERNAVN</label>
                <input asp-for="email.Lastname" required />
            </div>
            <div>
                <input type="submit" value="Send" />
            </div>
        </form>
        <partial name="/Pages/Shared/_FormScript.cshtml" />
    </div>
</section>

_Project.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
    <RootNamespace>AsonCore</RootNamespace>
  </PropertyGroup>

  <ItemGroup>
    <Content Remove="Pages\Partial\**" />
  </ItemGroup>

  <ItemGroup>
    <None Include="Pages\Partial\**">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
  </ItemGroup>

</Project>

At build the partial views are removed from the compiled views.dll to enable dynamic updates of their content, and when I did, what initially were inherited from _ViewImports.cshtml stopped.

By adding @namespace AsonCore.Pages to the partial view it picked up the Model, but then I noticed, after deploying to server (it works in VS2019 though), that "TagHelpers" stopped working.

Also adding @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers to the view fixed that, but my question is, is this how it need/should be done?

Or is there any other way to prevent specific views from being compiled, where the inheritance from "ViewImports" is preserved?

like image 461
Asons Avatar asked May 31 '19 08:05

Asons


People also ask

How to render partial view in Razor?

For this go to Solution Explorer then select Views -> Shared Folder -> Right-click -> Add View. Now for the View -> Home -> Index. cshtml. Here I am rendering a Partial View using 4 types, so the index.

How to render partial view in MVC. net core?

Declare partial views In ASP.NET Core MVC, a controller's ViewResult is capable of returning either a view or a partial view. In Razor Pages, a PageModel can return a partial view represented as a PartialViewResult object. Referencing and rendering partial views is described in the Reference a partial view section.

How partial view works in MVC?

To create a partial view, right click on the Shared folder -> click Add -> click View.. to open the Add View popup, as shown below. You can create a partial view in any View folder. However, it is recommended to create all your partial views in the Shared folder so that they can be used in multiple views.

What is partial view in Razor Pages?

Partial Pages or Views are Razor files containing snippets of HTML and server-side code to be included in any number of pages or layouts. Partial pages can be used to break up complex pages into smaller units, thereby reducing the complexity and allowing teams to work on different units concurrently.


1 Answers

I post this self-answer as a temporary workaround (still hope for one though, that will explain what goes on, and where to find it in official resources/docs).


It appears it is no more complicated than for a compiled partial view, it uses the compiled _ViewImports.cshtml, and for any uncompiled it needs a copy of the _ViewImports.cshtml in its default location, the Pages\ folder.

In my case something like this

\Pages\Partial\
  _Application.cshtml

\Pages\
  _ViewImports.cshtml

\
  AsonCore.Views.dll

It also turns out, that one can take any other compiled main view file (which weren't removed on publish, as I did with the partial view), copy it to the Pages\ folder, edit it if one want to, and it will reload and override the compiled one, that resides in the views.dll


The above alone will render in an exception though (when trying to execute uncompiled views at runtime), but with help from this answer it works, which says:

To correct this, you must to publish folder add refs subfolder with some runtime assemblies. This refs folder is normaly created if you do publish project with MvcRazorCompileOnPublish set to false. So you must one time do publish without precompilation to another folder and copy this refs subfolder from there.

  • ASP.NET Core 2.0 MVC - how to exclude some folders from View Compilation?

Note, there might be some other way than to use this refs folder in Core 2.2, and if someone know, please, feel free to edit my answer with that


Edit - ASP.NET Core 3.1

When it comes to the refs folder using 3.1, one just do this to get the runtime files copied when publish:

In startup class

services.AddRazorPages()
    .AddRazorRuntimeCompilation();

In .csproj file

<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.4" />
like image 86
Asons Avatar answered Sep 22 '22 14:09

Asons