Chronicling the Adventure that is software Development

RC2

Using ASP.Net Core 1.0 Visual Studio 2015 I will walk through how to create a Web API. We will add some basic documentation and hook it up with Swashbuckle and Swagger. The end result is gonna be AWESOME!

Setup the Project

Open up Visual Studio and create a new .Net Core.

Select the “Web API” template.

After the project has been created we need to tell it to export an XML document when it builds. This will allow us to generate a XML doc that will hold our swagger info and comments.
Right Click the Project > Select “Properties” > Select “Build” > check XML documentation file > Save

Setup Project.json and Startup.cs

Open up your project.json file. This will be new to you if you haven’t played with previous releases of vNext.

What is project.json?

Introduced in the first versions of ASP.Net vNext (which is now called ASP.NET Core), the project.json file defines your package metadata, your project dependencies and which frameworks you want to build for. (relatively similar to ASP.NET 4x’s package.config). You can remove or add a dependencies from this file. When you do, youre solution will automatically be updated to reflect those changes. You can also set what frameworks you want your app to compile across here! Note that Microsoft has stated that project.json may go away around the time Visual Studio 15 is released. The idea with that move is that settings will move back to the .csproj file to enable better integration with MSBuild in that time frame.

Since we selected the Web API template, the core API dependencies and tools were automatically added to both these files when this project was created. So all we need to do is add the Swashbuckle dependency to the project.json file.- “Swashbuckle”: “6.0.0-beta902”

Next, open up Startup.cs. In the ConfigureServices method you will need to know the path to your xml comments file which will be automatically generated for you. This path will come from GetSwaggerXMLPath(). Make sure to add the correct xml file name, it will be named [project name].xml. You also will need to add the swagger service and configure a few properties as shown below:

You should now be able to see your Swagger page. Run the project and navigate to http://localhost:XXXXX/swagger/ui/index.html

Add Swag Documentation to your API controllers!

First lets quickly go over what Swagger and Swashbuckle are:

Swagger is a simple yet powerful representation of your RESTful API. With the largest ecosystem of API tooling on the planet, thousands of developers are supporting Swagger in almost every modern programming language and deployment environment. With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability.

-swagger.io

Swashbuckle is basically, a implementation of Swagger for ASP.NET

In addition to its Swagger generator, Swashbuckle also contains an embedded version of swagger-ui which it will automatically serve up once Swashbuckle is installed. This means you can complement your API with a slick discovery UI to assist consumers with their integration efforts. Best of all, it requires minimal coding and maintenance, allowing you to focus on building an awesome API!

-Swashbucket git repo

Now, to get our shiny new Swagger UI to show documentation of our API lets open up ValuesController.cs and add a new method to play with its comments and attributes.

and if we try to pass it a non integer into this new endpoint we will get this:

Finished! Congrats!

There is still much to learn, I’d recommend checking out the dotNetConf videos for more .Net Core info. Also is should be noted that our project above just used the default response formatting which is JSON. The response data can be configured to things like XML by setting formatters in Startup.cs

After successfully running the above command, your project should now look something like this:

Run Auto Scaffold Command in Command Prompt (Second Option)

Another way to run these commands is through the Command Prompt. To run the scaffold command, open up your command prompt and navigate to the projects root folder. Then run a command that looks like this:

A quick walk through of ASP.NET Core 1.0 on how to set up a MVC application from a “Blank” project. Part 2, will demonstrate how to add Angular to your Core 1.0 App.

Create your web app!

Open up Visual Studio and select File > New > Project. If you have ASP.NET Core 1.0 or higher installed (here is the .Net Core Installation guide) you should see and exciting new category under Templates > Visual C# called .Net Core!

Setup Project.json

Open up your project.json file. This will be new to you if you haven’t played with previous releases of vNext.

What is project.json?

Introduced in the first versions of ASP.Net vNext (which is now called ASP.NET Core), the project.json file defines your package metadata, your project dependencies and which frameworks you want to build for. (relatively similar to ASP.NET 4.x’s package.config). You can remove or add a dependency from this file. When you do, you’re solution will automatically be updated to reflect those changes. You can also set what frameworks you want your app to compile across here! Note that Microsoft has stated that project.json may go away around the time the next version of Visual Studio is released. The idea is that settings will move back to the .csproj file to enable better integration with MSBuild in that time frame.

To create a basic MVC app you will need to add the following dependencies to project.json:

“Microsoft.AspNetCore.Mvc”: “1.1.0”

“Microsoft.AspNetCore.StaticFiles”: “1.1.0”

“Microsoft.Extensions.Configuration.FileExtensions”: “1.1.0”

“Microsoft.Extensions.Configuration.Json”: “1.1.0”

And you will need to make sure “buildOptions: preserveCompilationContext” is set to true. After making these changes, your project.json should now look like this:

This post is based on EF Core 1.0 and it will be strictly about adding SQL Views to your Model and Data Context. If you are interested on how to connect your EF Core 1.0 project to a SQL Server DB then check out this other post about how to reverse engineer and scaffold your SQL Server DB.

The state of EF Core 1.0 and VS2015
Unfortunately, that nice UI you were used too that allowed you to connect and select the tables and views to be mapped out, does not yet exist. After setting up your project and project.json file you will run a script in the Command Prompt or in the Nuget Package Manager to connect and build out you EF model. Will look a lil something like this:

The above commands hook you up to your DB and creates a Model folder that contains all your newly generated classes and DataContext.cs. But what about my views!?!? Well, since your DataContext.cs is connected to your DB, you do technically have access to everything in that DB, including SPROCs and Views. These Views just dont get created for you and there is no real way to access them except through inline SQL or manually adding them. So lets manually add them together!

Create a new Class for your View

Your project should have a Models folder (if you did everything here). Right click that Models folder and select Add > New Item.. > Class

Also, note that the DataContext is a “Shared” class. So if you don’t want to re-write this code every time you run the update scaffold command, putting code like this Team_View code in a separate shared DataContext.cs file may be the way to go.

Thats It!
You now have access you your View! AND you now have a Class for that view! You can now call it from any lambda/LINQ query that your want.