I've been writing numerous ASP.NET Core Tutorials to document my adventures learning ASP.NET Core MVC, Web API, etc. I've been starting these applications from scratch using Visual Studio Code on macOS. I've purposely avoided wizards so I know what's happening under the covers.

However, the various templates and wizards for ASP.NET Core MVC are great from a learning experience, too. Once you get a handle for the nitty gritty details it's time to jump into the boilerplate code generated by various ASP.NET Core MVC project templates. One can use the dotnet CLI to generate a more full-fledge ASP.NET Core MVC web application, and I will show you how to do that now.

ASP.NET Core MVC Web Template

Create a new directory for the ASP.NET Core MVC Web Application and switch to it. Execute the dotnet new command like I have shown in my other tutorials, but include the -t web arguments to specify the generation of an ASP.NET Core MVC Web Template. After that, execute the dotnet restore command to install the packages used for the ASP.NET Core MVC Website.

mkdir webapp && CD $_

dotnet new -t web

dotnet restore

At this point I don't even need to open up Visual Studio Code unless I want to learn from it. I can just run the ASP.NET Core MVC Web Application using dotnet run, which will both build and run the web application.

dotnet run

Project webapp (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling webapp for .NETCoreApp,Version=v1.0

Compilation succeeded.
    0 Warning(s)
    0 Error(s)

Time elapsed 00:00:02.4179025
 

Hosting environment: Production
Content root path: /Users/Sasquatch/Core/webapp
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

When I open my browser to http://localhost:5000, I see a more feature-rich ASP.NET Core MVC Sample.

ASP.NET Core MVC Web Template

The Startup Class alone shows how to configure various environmental settings, logging, database connections, authentication, and dependency injection.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using WebApplication.Data;
using WebApplication.Models;
using WebApplication.Services;

namespace WebApplication {
    public class Startup {
        public Startup(IHostingEnvironment env) {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

            if (env.IsDevelopment()) {
                // For more details on using the user secret store see https://go.microsoft.com/fwlink/?LinkID=532709
                builder.AddUserSecrets();
            }

            builder.AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services) {
            // Add framework services.
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.AddMvc();

            // Add application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment()) {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
                app.UseBrowserLink();
            }
            else {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseIdentity();

            // Add external authentication middleware below. To configure them please see https://go.microsoft.com/fwlink/?LinkID=532715

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

I'll explore a number of the features and middleware mentioned in this Startup Class in future ASP.NET Core MVC Tutorials.

I hope to see you on twitter, @KoderDojo!

Posted by Koder Dojo

Welcome to Koder Dojo. I am a C# ASP.NET MVC Developer learning ASP.NET Core MVC in the open. Numerous articles show step-by-step tutorials on developing ASP.NET Core MVC and ASP.NET Core Web API applications from scratch. Often I build these .NET Core Apps on macOS using Visual Studio Code to keep things real. I hope to see you on twitter: @KoderDojo.

Related Posts: