Create your first ASP.NET 5 (MVC 6) from scratch with Visual Studio 2015 [Part 1]

In this post, I will show you how to create an ASP.NET 5 (MVC 6) from the empty project template in VS 2015 (i used Community RC version). I also explain some new terms used in this new version of ASP.NET. Let’s get started.

My dev environment: Windows 8.1 OS, Visual Studio 2015 Community RC.

First of all, open your VS2015, and create a new blank ASP.NET 5 application.

aspnet5_beginning_demo_1

You can see the project structure like this

aspnet5_beginning_demo_2

Let’s use this shortcut Ctrl + F5 to build the project and see what happens with this kind of project. Bump.

aspnet5_beginning_demo_3

You will wonder how ‘Hello World!” appears. Open Startup.cs file, and you will see this method


public void Configure(IApplicationBuilder app)
{
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}

Let’s change the text, and build again to see the result.


await context.Response.WriteAsync("Hello Aliens!");

aspnet5_beginning_demo_4

Bump, I’m done. That’s it. Is it easy?😀

Next, I will explain to you about ‘wwwroot‘ folder, a term used in ASP.NET 5. As you saw in the project structure, you saw a strange folder called ‘wwwroot‘. In previous version of ASP.NET, the typical project is the root, but in this new version, the root of the application is ‘wwwroot‘. Let’s do some things fun with it.

Comment these lines in the Startup.cs file, say goodbye to aliens now😀, and build the project again.

public void Configure(IApplicationBuilder app)
{
//app.Run(async (context) =>
//{
// await context.Response.WriteAsync(“Hello Aliens!”);
//});
}

[\sourcecode]

aspnet5_beginning_demo_5

Ops, errorrrr…… I hate it.

Take back to the before-comment lines, those lines return a response with text “Hello Aliens!” to the client, and you can see it in the browser. After commenting those lines, IIS will look up files in the wwwroot folder to execute, but there’s nothing. So, that’s why you saw the error page above.

Let’s move Project_Readme.html file to the wwwroot folder, and navigate to that file on the browser and see what happens.

aspnet5_beginning_demo_6

aspnet5_beginning_demo_7

That page is showed up. If you move the Project_Readme.html file back to root folder of the project, the error page appears again.

One thing I want to show you here is how to use the middleware (OWIN, Katana) in the project. Open the project.json file, and add this line to ‘dependencies’ section.

aspnet5_beginning_demo_8

Open Startup.cs file, and add this line app.UseWelcomePage();

aspnet5_beginning_demo_9

Build project again to see what happens.

aspnet5_beginning_demo_10

A beautiful built-in page appears without coding😀.

In this version of ASP.NET, we can do self-hosting for the application, and it’s pre-defined in  file.

aspnet5_beginning_demo_11

In Visual Studio, we can choose hosting method for the project, IIS or web.

aspnet5_beginning_demo_12
The default is IIS, if you choose web, the application will read project.json file to get hosting information, and make a hosting for you. Let’s do it now.

From VS, switch to use ‘web’ to self-hosting the application, and build the project again to see what happens.

aspnet5_beginning_demo_13

aspnet5_beginning_demo_14

A console window appears with status line ‘Started’, it means that your hosting is started. Let’s navigate to the address defined in project.json to see what happens.

aspnet5_beginning_demo_15

Oh yeah, the same page appears.

In Part 2, I will show you how to add controller, view to build a website by your own.

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