Go to File -> New Project And create a Web Project:
Select Web API
It will create a solution with some files.
Run it and make sure it works!
Yay it works! Now lets go to the values controller
and disasble auth for now. Remove the [Authorize]
line
Now run the project again and naviage to http://localhost:port/api/values
.
Yay it works!
BUT it's XML and that's stupid. It will decide what to send back depending on the Accept
header of the request but for our own sanity let's disable XML. Open the file App_Start/WebAPiConfig.cs
Go to this line:
And add this code:
//remove XML Formatter
config.Formatters.Remove(config.Formatters.XmlFormatter);
Now run and go the the same URL:
Excellent! Now let's move on to
The API already has a database built in, but we'll likely want some extra features. So let's make the database auto-migrate on startup.
Open the Package Manager Console. A quick way to do this is to press Ctrl+Q
and start typing "Package Manager Console"
Now make sure the default project is correct (right now there is just one so okay):
And type in the box:
Enable-Migrations
VS will churn for a while and add a bunch of files. The new Configuration.cs
file will be opened. Save and build to make sure everything checks out. Next, we're going to make sure the database migrates whenever it is used.
Open Models/IdentityModel.cs
and look at ApplicationDbContext
. Add this static constructor at the top of the class:
static ApplicationDbContext()
{
var initializer = new System.Data.Entity.MigrateDatabaseToLatestVersion<ApplicationDbContext, Migrations.Configuration>();
System.Data.Entity.Database.SetInitializer(initializer);
}
Now the database will automatically apply new migrations.