How to share classes/records with `Microsoft.AspNetCore.Mvc.FromQuery` attribute across .NET MAUI client and ASP .NET server projects?

4 weeks ago 27
ARTICLE AD BOX

The following class:

public abstract record TasksTransactions { public abstract record RetrievingOfSelection { public const string URN_PATH = "/api/tasks/selection"; public record QueryParameters { public ForcedFiltering? forcedFiltering { get; set; } public record ForcedFiltering { [Microsoft.AspNetCore.Mvc.FromQuery(Name = "ff.s")] public required TaskGateway.SelectionRetrieving.RequestParameters.ForcedFiltering.ProgressStatues progressStatus { get; set; } } public OptionalFiltering? optionalFiltering { get; set; } public record OptionalFiltering { [Microsoft.AspNetCore.Mvc.FromQuery(Name = "of.sd")] public DateOnly? startingDate { get; set; } [Microsoft.AspNetCore.Mvc.FromQuery(Name = "of.ed")] public DateOnly? endingDate { get; set; } [Microsoft.AspNetCore.Mvc.FromQuery(Name = "of.sr")] public string? searchingByFullOrPartialTitleOrDescription { get; set; } } } } }

is in C# project (let us call it "Shared") with following .csproj file:

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net9.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <ProjectReference Include="..\..\..\CommonSolution\CommonSolution.csproj" /> </ItemGroup> </Project>

Above class must be accessible from both of the following projects:

.NET MAUI client

AST .NET backend

I will append the .csproj files of both of them in the end of the question.

Currently, Microsoft.AspNetCore is not accessible from "Shared" project:

Cannot resolve symbol 'AspNetCore'

but if temporary comment out all usages of `Microsoft.AspNetCore`, my application will work.

Installing Microsoft.AspNetCore.Mvc to "Shared" project:

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net9.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <ProjectReference Include="..\..\..\CommonSolution\CommonSolution.csproj" /> </ItemGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.3.0" /> </ItemGroup> </Project>

Now, Microsoft.AspNetCore is accessible from "Shared" project, but if try to build the solution, it will fail with:

Severity Code Description Project File Line Suppression State Details

Error (active) CS0234

The type or namespace name 'ApplicationPartAttribute' does not exist in the namespace 'Microsoft.AspNetCore.Mvc.ApplicationParts' (are you missing an assembly reference?) Client (net9.0-ios) D:\XXX\Implementation\Elements\Client\obj\Debug\net9.0-ios\iossimulator-x64\Client.MvcApplicationPartsAssemblyInfo.cs 14

enter image description here

It to uninstall Microsoft.AspNetCore and temporary comment out all usages of Microsoft.AspNetCore, solution will be built without errors again. How to fix it?

Appendix

.csproj File of Client Project

Mainly been generated by Visual studio for .NET MAUI + Blazor hybrid application.

<Project Sdk="Microsoft.NET.Sdk.Razor"> <PropertyGroup> <TargetFrameworks>net9.0-maccatalyst;net9.0-ios;net9.0-android</TargetFrameworks> <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net9.0-windows10.0.19041.0</TargetFrameworks> <!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET --> <!-- <TargetFrameworks>$(TargetFrameworks);net6.0-tizen</TargetFrameworks> --> <OutputType>Exe</OutputType> <RootNamespace>Client</RootNamespace> <UseMaui>true</UseMaui> <SingleProject>true</SingleProject> <ImplicitUsings>enable</ImplicitUsings> <EnableDefaultCssItems>false</EnableDefaultCssItems> <Nullable>enable</Nullable> <!-- Display name --> <ApplicationTitle>Client</ApplicationTitle> <!-- App Identifier --> <ApplicationId>com.companyname.client</ApplicationId> <ApplicationIdGuid>2302D388-EFAC-4FCF-A8B6-3100441C5C7E</ApplicationIdGuid> <!-- Versions --> <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion> <ApplicationVersion>1</ApplicationVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">24.0</SupportedOSPlatformVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion> <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion> <!-- The bug workaroud. See https://github.com/dotnet/maui/issues/12080#issue-1495748327 --> <WindowsAppSdkDeploymentManagerInitialize>false</WindowsAppSdkDeploymentManagerInitialize> </PropertyGroup> <ItemGroup> <!-- App Icon --> <MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" /> <!-- Splash Screen --> <MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" /> <!-- Images --> <MauiImage Include="Resources\Images\*" /> <MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" /> <!-- Custom Fonts --> <MauiFont Include="Resources\Fonts\*" /> <!-- Raw Assets (also remove the "Resources\Raw" prefix) --> <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\..\..\Incubators\Utils\Utils.csproj" /> <ProjectReference Include="..\..\..\Incubators\FrontEndFramework\FrontEndFramework.csproj" /> <ProjectReference Include="..\..\Interactions\ClientAndFrontServer\ClientAndFrontServer.csproj" /> <ProjectReference Include="..\..\Reusables\EntityFramework\EntityFramework.csproj" /> <ProjectReference Include="..\..\Reusables\MockDataSource\MockDataSource.csproj" /> </ItemGroup> <ItemGroup> <PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.0" /> <PackageReference Include="Microsoft.Maui.Controls" Version="9.0.12" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="9.0.12" /> <PackageReference Include="YamatoDaiwaCS_Extensions" Version="0.2.0" /> <PackageReference Include="YamatoDaiwaFrontend" Version="0.3.4" /> </ItemGroup> </Project>

.csproj File of Server Project

Mainly been generated by Visual studio for ASP .NET server application.

<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net9.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <UserSecretsId>ae619771-2592-4d7a-82ae-f1c264e1968e</UserSecretsId> <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> <DockerfileContext>..\..\..</DockerfileContext> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\..\..\CommonSolution\CommonSolution.csproj" /> <ProjectReference Include="..\..\Interactions\ClientAndFrontServer\ClientAndFrontServer.csproj" /> <ProjectReference Include="..\..\Reusables\EntityFramework\EntityFramework.csproj" /> <ProjectReference Include="..\..\Reusables\MockDataSource\MockDataSource.csproj" /> </ItemGroup> </Project>
Read Entire Article