MVC6 Web API Attribute Routing

Attribute routing preferred to table routing

It seems that attribute routing is the default and preferred way to write web API’s now – you need to use a shim to get table routing to work. This may change – obviously ASP.NET 5 is still in beta.

New [controller] token

At the end of my last post, we had the following controller that give a list of customers in a web api:

In MVC6, you could replace

[Route(“Customers”)]

with

[Route(“[controller]”)]

This will use the name of the class minus the “Controller” suffix for the route of the API. It helps keep the route and class names in sync. Here’s the controller written in this way:

New [action] token

Let’s say we needed to implement a RPC style API, customers/GetFirstCustomer, to get the first customer in our list. We could implement this as follows, hardcoding the route name.

In MVC6, you could replace

[Route(“GetFirstCustomer”)]

with

[Route(“[action]”)]

This will use the name of the method for the route of the API. It helps keep the route and method names in sync.

Here’s the controller written using [controller] and [action]

Connect with me:RSSGitHubTwitterLinkedIn

Getting up and running with ASP.NET MVC 6

In this post, I’m going to detail how to get up and running with a very simple MVC 6 app containing a simple page and web API …

MVC 6 = MVC + Web API

A major benefit of ASP.NET 5 is that the programming model now combines MVC and Web API in single framework. In previous versions of ASP.NET there was overlapping features in MVC and Web API, but the implementation for both frameworks was totally different.

Create an empty web project

When you create a new project, select ASP.NET Web Application from the list of templates. Don’t worry about the framework version on this dialog.

new asp.net app

In this example we are then going to select ASP.NET 5 Preview Empty from the list of ASP.NET templates.

new asp.net blank app

Add MVC dependency

We now need to add the MVC dependency in project.json.

project.json dependency

Configure startup

The entry point for an ASP.NET 5 app happens in Startup.cs.

The ConfigureServices method is used to set up the services that the app needs to use – as the name suggests! We use services.AddMvc() in this method to add the MVC service to the app.

The Configure method is used to define what happens during the request pipeline. We use app.UseMvc() in this method to instruct the request pipeline to use the MVC route middleware using the standard routes.

Add controller

Add a new folder called “Controllers” and add a new MVC controller into it called “HomeController”.

add new controller

Add the “Route” attribute to define that this is the controller for the “Home” route.

Add web page

Add a new folder called “View” and a sub folder called “index” and add a new MVC view into it called “Index”.

add new view

simply add some header text into the view.

When you browse to /home in a browser, you should receive the home page.

home page

Add web API

Add a new controller called “CustomersController” and paste in the following code. This controller defines a web API for GET customers/id

If you browse to /customers/2 in chrome, you should get customer bob.

web API GET

Connect with me:RSSGitHubTwitterLinkedIn

ASP.NET 5 hello World

In this post, we’ll create an ASP.NET 5 app that contains a page containing “hello world” and then change the app to use the ASP.NET welcome page …

Create an empty web project

When you create a new project, select ASP.NET Web Application from the list of templates. Don’t worry about the framework version on this dialog.

new asp.net app

In this example we are then going to select ASP.NET 5 Preview Empty from the list of ASP.NET templates.

new asp.net blank app

Configure startup

The entry point for an ASP.NET 5 app happens in Startup.cs.

The ConfigureServices method is used to set up the services that the app needs to use – as the name suggests!

The Configure method is used to define what happens during the request pipeline. We can use IApplicationBuilder.Run() in order to do a very simple hello world.

If you run the project, you should receive “Hello World” on a web page.

Using a welcome page

We can use the standard ASP.NET welcome page instead of our “Hello World” page by instructing the request pipeline to do so:

For this to compile, you’ll need to add a dependency on Microsoft.AspNet.Diagnostics

Now, when you run the project, you should receive the ASP.NET welcome page.

Connect with me:RSSGitHubTwitterLinkedIn

ASP.NET config.json

In my last post, I briefly mentioned config.json in ASP.NET 5. In this post, I am going to drill into configuration and config.json a bit more …

Configuration Source

ASP.NET 5 configuration is a lot more flexible – in particular, the way you can have configuration settings coming from different sources. In the following example we are going to get a connection string from config.json.

Firstly, we need to declare a dependency so that we can easily read from config.json:

dependency

Then we wire up config.json in the constructor of Startup.cs:

startup

Reading Configuration Settings

Let’s say we have the following config.json that defines the connection to our database:

config.json

In order to read the connection string we use IConfiguration.Get():

read

Notice how you use a colon to get to the nested value in the object graph.

Other Articles

Louis Dejardin explains the reasons for the changes in ASP.NET configuration and this post from Jaspalsinh Chauhan is another great article on this topic.

Connect with me:RSSGitHubTwitterLinkedIn

ASP.NET 5 Solution Tree

In the last few weeks I’ve started to look at ASP.NET 5. There are lots of differences to the ASP.NET we have now – the one that hit me straight away was the solution tree …

v4 v v5

global.json

In short, this is used to define all the projects in the solution. In the example below, two projects are defined for the main source code and the unit tests.

global.json

wwwroot

This is a new folder and it is where you should put all files that need to be served. So, for example, static HTML files would go in this folder. Your source code, on the other hand, wouldn’t go in this folder. I quite like this – it gives a nice separation between your source code and the stuff that needs to be served.

References

This node in the solution tree exists in v4.x and v5 and the purpose is the same – to show the .NET references. However, v5 shows the references in a hierarchical format:

references

project.json

project.json contains a lot of the stuff that used to be in web.config.

project.json

In the screenshot above:

  • webroot allows you to rename the webroot node within the tree
  • version is the version of the app / component
  • dependencies are the .NET dependencies in the project
  • frameworks are what framework compilations need to be done
  • bundleExclude are the folders to exclude during the bundling process
  • exclude are the folders to exclude during the publishing process

This wiki details more options that are available in project.json.

config.json

config.json contains app settings like the database connection string.

config.json

By default, when you create a blank project, config.json is not included. However, you can add it by selecting “ASP.NET Configuration File” when you add a new item to the project.

ASP.NET Configuration File

Startup.cs

Startup.cs replaces Application_Start() in Global.asax and allows you to set up dependent services when the app boots up. Shawn Wildermuth has a great post on Startup.cs.

Connect with me:RSSGitHubTwitterLinkedIn