How to build an ASP.NET Core 6 website that also contains a WebAPI?

Jeff Bowman 106 Reputation points
2022-03-30T22:12:18.587+00:00

I'm new to ASP.NET Core 6, coming from a solid WebForms background.

In my searches, I'm finding many results demonstrating how to build an ASP.NET Core 6 website, and likewise many demonstrating how to build an ASP.NET Core 6 WebAPI. But I'm not finding anything showing how to do both in the same project, or even whether it's possible at all.

Given the terrible news of WCF's demise, we're relegated to using inferior replacements. gRPC fails the requirement, as it will run on only the very latest versions of IIS that support HTTP/2. It seems the only remaining alternative is WebAPI.

Is it possible to build an ASP.NET Core 6 website that also contains a WebAPI, similar in concept to the old in-website WCF service? If so, how is it done?

Some beginning guidance would be very helpful.

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

Accepted answer
  1. Anonymous
    2022-03-31T02:20:52.953+00:00

    Hi @Jeff Bowman ,

    How to build an ASP.NET Core 6 website that also contains a WebAPI?

    If you are using Visual Studio 2022 to create the application, you can use the Asp.net core MVC template to create a MVC application:

    188602-image.png

    Then, right click the Controller folder, and choose Add => Controller option:

    188564-image.png

    In the popup window, you can choose the API controller:

    188546-image.png

    Or, you can right click the Controllers folder, click Add => New Item... => select the API controller.

    188437-image.png

    After running the application, the result like this:

    188577-image.png

    You can also use JavaScript or Httpclient to access the API from the MVC view or controller.

    Here are some basic tutorial about MVC and API, you can refer to them:

    Create web APIs with ASP.NET Core

    Get started with ASP.NET Core MVC

    Tutorial: Call an ASP.NET Core web API with JavaScript

    Make HTTP requests using IHttpClientFactory in ASP.NET Core.


    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.

    Best regards,
    Dillion


2 additional answers

Sort by: Most helpful
  1. Lex Li (Microsoft) 6,037 Reputation points Microsoft Employee
    2022-03-31T00:05:26.483+00:00

    https://learn.microsoft.com/en-us/dotnet/architecture/porting-existing-aspnet-apps/webapi-differences

    "In ASP.NET Core, there's no longer any distinction between MVC and Web APIs. There's only ASP.NET MVC, which includes support for view-based scenarios, API endpoints, and Razor Pages (and other variations like health checks and SignalR)." Starting from ASP.NET Core 1.0, a long while ago.


  2. Jeff Bowman 106 Reputation points
    2022-03-31T00:19:30.01+00:00

    Thanks, I'll have a look.

    0 comments No comments

Your answer

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