Uncompiled partial view doesn't inherit from ViewImports
Asked Answered
C

1

16

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?

Calderon answered 31/5, 2019 at 8:28 Comment(0)
C
2

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.

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" />
Calderon answered 10/6, 2019 at 8:7 Comment(1)
FYI, you can set CopyRefAssembliesToPublishDirectory to true in the .csproj file instead of setting up rules to copy the files manually, etc. The MSBuild properties are documented here.Orchestrion

© 2022 - 2024 — McMap. All rights reserved.