Start web api in Console Application?

mc 6,396 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?

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    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

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.