Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create WebApplication extension method in external library

Tags:

c#

.net

I'm trying to create an extension method to register some routes in an external library using C# .NET 6.

I can'f find a way to do it because i cannot reference Microsoft.AspNetCore.App.Ref where the WebApplication class is defined.

Has anyone encountered this problem and found a solution?

like image 706
nano Avatar asked Aug 03 '26 08:08

nano


1 Answers

If I undertand your problem correctly:

  1. You have a class library project (i.e. your .csproj file starts with
<Project Sdk="Microsoft.NET.Sdk">
  1. You want to create an extension method to WebApplication like
public static class SomeModule
{
  public static void DoSomething(this WebApplication webApplication)
  {
    // stuff
  }
}

As you can't reference Microsoft.AspNetCore.App.Ref you can try with adding a framework reference to your project as per Use the ASP.NET Core shared framework article.

<Project Sdk="Microsoft.NET.Sdk">
    
    <ItemGroup>
        <FrameworkReference Include="Microsoft.AspNetCore.App" />
    </ItemGroup>

    <!--Rest of your project stuff-->
</Project>

and this way you're good to go.

like image 142
Piotr Lasota Avatar answered Aug 05 '26 11:08

Piotr Lasota



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!