DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Create a Minimal ASP.NET Core 2.0 MVC Web Application

Create a Minimal ASP.NET Core 2.0 MVC Web Application

In this tutorial, we'll show you how to create a basic ASP.NET Core 2 web app from setting up the template, to adding some C#, to finishing it off with a little HTML.

$$anonymous$$ user avatar by
$$anonymous$$
·
Aug. 29, 17 · Tutorial
Like (4)
Save
Tweet
Share
21.71K Views

Join the DZone community and get the full member experience.

Join For Free

This guide shows you how to set up an ASP.NET Core 2.0 MVC web application from an empty template.

You can create a project using the MVC template in .NET Core but it will often include a load of things that we don't need. My preference is always to start a project from scratch.

Here are the steps to go from an empty project to an MVC ready project.

Requirements

You will need to install the following:

First, create a new ASP.NET Core Web Application and choose the empty template, making sure you are targeting ASP.NET Core 2.0.

The empty project contains little more than wwwroot, Program.cs and Startup.cs. It may seem a little unusual that Program.cs is in there but web apps are now, essentially, console applications and contain a Program.cs just the same as a console app would. This means web apps can now be run from the command line.

The default code in Startup.cs contains:

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

This writes "Hello World!" in the response stream. Press F5 and you should see the result in the browser.

Now install MVC via NuGet Package Manager:

Install-Package Microsoft.AspNetCore.Mvc -Version 2.0.0 

Once installed, there are a couple of things we'll need to add to Startup.cs. Firstly, configure the app to use the MVC framework.

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

Secondly, configure the app to use the MVC request execution pipeline (routing), with a default route.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{ 
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });

    // ...
}

Now that the MVC framework has been added, let's add a Controller and a View!

Add a new folder called Controllers to the project and right click to add a Controller.

As we are going for a minimal install we'll select Minimal Dependencies, as we only want to install the minimum required.

This adds scaffolding to the project and now the MVC templates for Controllers and Views will work. When it has finished, right click and add a Controller again.

Choose an empty MVC template and call it HomeController. The controller will be added with a default method called Index. Right click on View() and select AddView...

When the scaffolding creates the view it will also create the correct folder structure for us. You will see that Index.cshtml is located in Views -> Home.

Add a little HTML into Index.cshtml

<h1>Hello Bacon!</h1> 

And then run the app, it should show in the browser, like so:

And we're done!

Summary

Setting up a project from an empty template is always my preferred approach and as you can see it is very simple and quick to set up an MVC app using ASP.NET Core 2.0. Give it a try!

ASP.NET ASP.NET Core Web application

Published at DZone with permission of $$anonymous$$, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Future of Cloud Engineering Evolves
  • Better Performance and Security by Monitoring Logs, Metrics, and More
  • Kubernetes vs Docker: Differences Explained
  • OpenID Connect Flows

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: