Carl Rippon

Building SPAs

Carl Rippon
BlogBooks / CoursesAbout
This site uses cookies. Click here to find out more

Getting up and running with ASP.NET MVC 6

March 23, 2015
dotnet

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.

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseMvc();
    }
}

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.

[Route("Home")]
public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

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.

<h1>home page</h1>

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

[Route("Customers")]
public class CustomersController : Controller
{
    private List<Customer> _Customers = new List<Controllers.Customer>();

    public CustomersController()
    {
        _Customers.Add(new Customer() { ID = 1, Name = "Fred" });
        _Customers.Add(new Customer() { ID = 2, Name = "Bob" });
        _Customers.Add(new Customer() { ID = 3, Name = "Tim" });
    }

    [HttpGet("{id:int}")]
    public Customer GetCustomer(int ID)
    {
        Customer Customer = _Customers.Find(c => c.ID == ID);
        return Customer;
    }
}

public class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
}

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

web API GET

If you to learn about using React with ASP.NET Core you might find my book useful:

ASP.NET Core 5 and React

ASP.NET Core 5 and React
Find out more

Want more content like this?

Subscribe to receive notifications on new blog posts and courses

Required
© Carl Rippon
Privacy Policy