5

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.

Venkata Dorisala
  • 4,783
  • 7
  • 49
  • 90
Shaine Fisher
  • 315
  • 1
  • 3
  • 20

1 Answers1

13

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.

Andrei
  • 42,814
  • 35
  • 154
  • 218
  • Well I created my class, added a scaffolding item, set the context etc, and then I got this... There was an error creating the DbContext instance to get the model. No parameterless constructor defined for this object. – Shaine Fisher Jul 08 '16 at 19:23
  • Or, create controller, API Controller with Actions using Entity Framework, set context, same error. – Shaine Fisher Jul 08 '16 at 19:26
  • You were right, just selected the item API Controller with read/write actions, and it did it, thank you :) Answer accepted. – Shaine Fisher Jul 08 '16 at 19:27
  • So how do I access my api then? I tried /api/Tests and it offers to download a json file called Tests.json ?? – Shaine Fisher Jul 08 '16 at 19:38
  • 1
    How do you authenticate API requests? – poppertech Jul 08 '16 at 19:42
  • @ShaineFisher did you try in a different browser? – Andrei Jul 08 '16 at 19:46
  • @AndreiM thanks, sorted using Edge and Chrome instead of IE. – Shaine Fisher Jul 08 '16 at 20:16
  • @bwyn I would assume (not tested) you use the Authorize tag on the method in the controller yopu want to authorize, in my old projecxt I had read as Anon, write as Authorize and Delete and Authorize, Admins. Hoping that ports to this version too. – Shaine Fisher Jul 08 '16 at 20:16
  • @ShaineFisher yes, most likely Authorize will work. You will have to configure your authorization in application Startup class. – Andrei Jul 08 '16 at 20:18
  • In previous versions of Web Api, the client passed a bearer token to the server for authentication. How are you going to pass credentials to the server? – poppertech Jul 08 '16 at 20:33
  • At this point, this is an experiment, will let you know tomorrow when I have figured it out. – Shaine Fisher Jul 08 '16 at 21:50
  • @bwyn this may be harder than it looks on paper – Shaine Fisher Jul 09 '16 at 09:53
  • I am having a hard time finding resources for this as well. The following post looks promising http://stackoverflow.com/questions/30546542/token-based-authentication-in-asp-net-5-vnext-refreshed (particularly some of the newer answers), but I have not tried them yet. – poppertech Jul 09 '16 at 10:19