Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting Views and Controllers in a different project than the ASP.NET Core Web app

I am trying to split up my ASP.NET Core 3.0 pre-release 9 MVC app into different projects. For example, I’d like to have everything related to one subdomain go into a project called Website.SubdomainA and the other one into Website.SubdomainB.

I tried to accomplish this with a Razor Class Library. According to MSDN, these should be able to contain the follwing:

Razor views, pages, controllers, page models, Razor components, View components, and data models can be built into a Razor class library (RCL).

So I went ahead and created a 'RCL', which looks something like the following:

<Project Sdk="Microsoft.NET.Sdk.Razor">
  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <RazorLangVersion>3.0</RazorLangVersion>
  </PropertyGroup>
</Project>

Then I copied over my Controllers and Views—but, for some reason, it can't find any of the assemblies which contain the base classes like Controller, RouteAttribute and all the others which are included in the Microsoft.AspNetCore.Mvc namespace.

Further, my Views do not compile since they can't find the needed assemblies, and things like Layout = "Test" get marked red.

Am I missing something obvious?

like image 367
Twenty Avatar asked Sep 07 '19 21:09

Twenty


1 Answers

For creating Razor Class Library, you need to append AddRazorSupportForMvc in the *.csproj like below:

<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <AddRazorSupportForMvc>true</AddRazorSupportForMvc>
</PropertyGroup>
<ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>
like image 119
Edward Avatar answered Nov 09 '22 14:11

Edward