I have created a new Project, selected ASP.NET Core Web Application (.NET Core), then selected Web Application, Individual User Accounts.
All is good and the project works perfectly, but I want to add WebAPI functionality to this project, so that http://website.com/Home is MVC, and http://website.com/api/whatever is my api, I would like them both to use the same authentication database (so you can register on the MVC site and authenticate to the API).
I do not want to add a second project to the solution if I can avoid it.
Someone posted how to add WebAPI 4 to an existing MVC project but those instructions failed at step 1, add x to the Global.asax, which doesn't exist for an ASP.Net Core MVC Project.
Please help.
In simple words, we can say that a web API is an application programming interface for a web application or web server. It uses HTTP protocol to communicate between clients and websites to have data access. Asp.net Core web API is a cross-platform web API.
Your ASP.NET Core Controller
already supports both MVC and WebAPI. In ASP.NET Core these frameworks were combined together. So all you need is declare a controller action but instead of returning ViewResult return your REST API model.
Example:
public class ValuesController : Controller
{
[HttpGet]
public List<string> Get()
{
return new List<string> { "Hello", "World" };
}
[HttpGet]
[Route("values/{valueName}")]
public string Get(string valueName)
{
return valueName;
}
}
It must be accessible under GET: /api/values
and GET /api/values/ + valueName
, depending on configuration. These are most common use cases.
Good luck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With