An Introduction to ASP.NET Core

ASP.NET Core uses .NET Core, and is the next generation of ASP.NET, having first been released only last year (2016).

ASP.NET itself uses the full .NET Framework, and has been around since .NET’s first release back in 2002.

In this article, I’ll provide a quick introduction to ASP.NET Core. If you already know about ASP.NET MVC, you won’t learn anything new here!

Note that I’ll use the term ‘web application’ rather than ‘website’, because web applications can be websites or web APIs, running either on the internet or on a private intranet. (A web API, if you’re unaware, can be considered a set of functions accessed across the internet or other network.)

Why .NET Core?

.NET Core – used by ASP.NET Core, naturally – is the next generation of the .NET Framework. It’s open source, cross-platform (Windows, Linux, Mac), highly performant, and getting better with every release. There’s virtually nothing bad anybody can say about it!

An Overview of MVC

You’ve almost certainly heard of MVC. In web development, it’s everywhere, and it’s not specific to ASP.NET, ASP.NET Core, or any other web technology.

Model-View-Controller (MVC) is an architectural pattern that splits an application into discrete parts according to their role. It’s not new, it’s been around for a long time, but it gained popularity particularly in the development of web applications.

Models

Models represent data. In an MVC application, there are two types of model: the model that represents data stored in database tables; and ‘view models’, which are used to transfer data to and from web pages.

Views

Views represent web pages, and web pages make up a web application. Users interact with views. Data (in the form of a view model) is passed to a view before it’s returned to the user’s browser, and passed back when the user submits a form.

Controllers

Controllers represent the logic behind a web application. When a user requests a web page, a controller picks up the request. The controller might perform some validation, processing, or other tasks, but ends by passing data (in the form of a view model) to a view. The view is then returned to the user’s browser.

Interaction

Here’s the obligatory diagram that shows the interaction between the parts of an MVC application:

MVC Architecture Diagram

It’s grossly simplified and high-level, but that’s the beauty of the MVC architecture: it is conceptually simple. If you’re coming from a Web Forms background, then MVC is radically different and adheres nicely to the separation of concerns (SoC) principle.

MVC Operations

Here’s a quick overview of how data is passed around in an MVC application.

Passing data to a view

The controller can trigger the rendering of a view by returning the result of calling the View() method. Any object can be passed to the view, and that object becomes the view model that the view uses to help render the HTML page.

public IActionResult Index()
{
    return View(new IndexViewModel
    {
        Name = "Dave"
    });
}

In this case the view model is actually a POCO (a class with no methods) defined as follows:

public class IndexViewModel
{
    public string Name { get; set; }
}

When a user navigates to the index page of the controller – e.g. http://example.com/controller/index or just http://example.com/controller – the controller’s Index() method is executed. This returns a View object (by default, it’ll return a view of the same name as the method; Index in this example). Optionally, a view model can be attached to the view, and the view (implemented using a .cshtml file) can use the data provided by the view model as simply as this:

Hello @Model.Name

At the head of the view (the .cshtml file) the view model must be referenced:

@model WebApplication1.Models.IndexViewModel

Sending data to a controller

There are (at least) two ways in which a page can send data to the page’s controller.

Forms

Typically, the user enters some text into an input control, then clicks a button which submits a form. The data is passed to the controller in the form of a view model.

<form action="/Home/Submit">
    <label>Your name is:</label> <input name="Name" type="text" />
    <button type="submit">Submit</button>
</form>

A method is added to the controller to handle the form submission:

public IActionResult Submit(IndexViewModel viewModel)
{
    return View(viewModel);
}

Remember that View() will cause a view (with the same name as the method unless specified) to be rendered.

Submit.cshtml must reference the view model at the top:

@model WebApplication1.Models.IndexViewModel

and the view model can be used:

Hello @Model.Name

(This is just the same as the Index view above!)

So the user enters their name in the text box, then presses Submit. The form’s action determines the controller (Home) and the page (Submit). So the HomeController’s Submit() method is called, with a view model created (from the contents of the form) and passed to the method. All our method does is return the view model, so that the Submit view can say hello.

There’s a lot going on here, and we’ve only just scratched the surface.

JavaScript

Another way in which data can be sent to a controller is by using JavaScript. But that’s beyond the scope of this short intro to ASP.NET Core. Maybe next time.

One thought on “An Introduction to ASP.NET Core

  1. Pingback: Using JavaScript with ASP.NET Core MVC – A blog for software developers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s