0

I have create an "ASP.NET Core Web App" Project in Visual Studio.

I want to add an API in this project ? I can create a separate Web API project but i want to put everything in the same project.

Is it possible ?

Thanks

6
  • Is it possible ? - Yes. Commented Jun 17, 2021 at 13:51
  • Did you intend to create a Razor Pages project? And where does Blazor come in? Commented Jun 17, 2021 at 14:10
  • Non this is not a "Razor Pages" project but "ASP.NET Core Web App" Commented Jun 17, 2021 at 15:57
  • As Henk asked, where does Blazor fit in? And if it does fit in, is it WebAssembly or server-side? The answer to your question will depend on these details. Commented Jun 17, 2021 at 16:13
  • This is not a blazor project. This is an ASP.Net Core Web App. So it is a full server app Commented Jun 17, 2021 at 18:09

2 Answers 2

4

If you want to keep the web app project and also add a few API endpoints you could just add a folder (e.g. Api) at the project root where you can place your api-controllers. Then in that folder you can add a class that will derive from ControllerBase and decorate it with [ApiController] attribute and a [Route] attribute.

This requires configuration of controllers in your Startup.cs though:

  1. In ConfigureServices() add a call to services.AddControllers();
  2. In Configure specify the MapControllers() option:
app.UseEndpoints(endpoints => {
   // other code
   endpoints.MapControllers();
});
Sign up to request clarification or add additional context in comments.

Comments

-2

I would suggest you build an API solution, and then a separate Core Web App solution.

Otherwise you could try starting with a 'Blank Solution' and adding different projects to it.

1 Comment

I got your downvote right here, buster - CodeDonkey

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.