Start web api in Console Application?

mc 3,726 Reputation points
2023-11-27T03:41:25.3433333+00:00

I want to build WebApplication in Console Application.

<Project Sdk="Microsoft.NET.Sdk">

Can I use WebApplication.CreateBuilder in it?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,239 questions
0 comments No comments
{count} votes

Accepted answer
  1. Brando Zhang-MSFT 2,966 Reputation points Microsoft Vendor
    2023-11-27T07:03:50.3+00:00

    Hi @mc,

    Can I use WebApplication.CreateBuilder in it?

    You should use Microsoft.NET.Sdk.Web instead of Microsoft.NET.Sdk, since the Microsoft.NET.Sdk doesn't contains the web related package.

    More details, you could refer to this article.

    If you want to run web application in console application, you should firstly modify the csproj as below to use Microsoft.NET.Sdk.Web.

    <Project Sdk="Microsoft.NET.Sdk.Web">
    

    Then you could use WebApplication.

    var builder = WebApplication.CreateBuilder(args);
    var app = builder.Build();
    app.MapGet("/", () => "Hello World!");
    app.Run();
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful