Search code examples
c#nugetasp.net-core-3.1razor-component-library

Add controller to a Razor Component Library for .Net Core 3.1


I am migrating to MVC Core 3.1 and thankfully it wasn't that hard until I had to migrate the RCL.

Within the RCL I am including some common controllers and in order to use the Controller base class and IActionResult type I was using the Nuget Microsoft.AspNetCore.Mvc.Abstraction but it's only available for .net Core 2.2.

Why I cannot find the newer version 3.1 in the Nuget repository?

When I navigate to the definition of IActionResult I am shown a file with version 3.1.0 so I suppose there should be a way to reference that instead of 2.2.


Solution

  • The project was based on NetStandard2.0 and I manually changed that to netcoreapp3.1 plus adding the reference to Microsoft.AspNetCore.App.

    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    

    To been able to use Controllers and any other feature of the MVC framework on 3.1

    • Create a RCL project
    • Edit the .csProj file and make the appropriate changes to look like that:
    <Project Sdk="Microsoft.NET.Sdk.Razor">
      <PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <RazorLangVersion>3.0</RazorLangVersion>
        <AddRazorSupportForMvc>true</AddRazorSupportForMvc>
      </PropertyGroup>
      <ItemGroup>
        <FrameworkReference Include="Microsoft.AspNetCore.App" />
        <PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.3" />
        <PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.3" />
      </ItemGroup>
    </Project>