This post is an addition to the 5 part tutorial series on getting started with ASP.NET MVC. This isn't really another part, but a prequel describing setting up a basic ASP.NET MVC project. This will be helpful if you're using an alternative to Visual Studio, such as VS Code (which is available for Linux, Mac, or Windows), or if you just want to know more about how to do things without the use of templates in Visual Studio.

The Files

The first file we'll look at is the Program.cs file, which contains the start up code for our web application:

Program.cs

csharp
using System.IO;
using Microsoft.AspNetCore.Hosting;

namespace MeowWorld
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();

host.Run();
}
}
}

This will build a new WebHost object, then run it.

We want it to use Kestrel and Internet Information Services (IIS). IIS is Microsoft's main server software and most likely what you're using to run this application in a Windows environment. Kestrel is a lightweight server that can run on a variety of environments. Kestrel lacks many of the features of IIS and Microsoft suggests running both IIS and Kestrel for this reason. Depending on your situation, you may want one or the other, or both. I recommend both unless you have a lot of experience with this already.

By default, we set the root directory to the current directory, but we can change that to another path if we want to store our web content elsewhere.

Finally we set a startup object that contains methods to customize the WebHost object more. These methods can be moved to the Program.cs file and that can be used as the StartUp class, but Visual Studio puts them in their own file for organization purposes, so that's what I'll be doing too.

For changing the port setting on this file to work in OpenShift, see Part 3.

Startup.cs

csharp
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace MeowWorld
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

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

Here we have two methods for setting up the WebHost. We use the ConfigureServices method for adding services to be used and the Configure method to configure them and other settings.

For example, if you look at the if check in the Configure method, you'll see how it checked if this is a development build and if so, shows the error 500 page that contains info like a stack trace.

Now here's where things can get a little overwhelming.

project.json

json
{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
},
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.AspNetCore.Mvc": "1.0.1",
},

"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},

"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},

"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},

"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},

"publishOptions": {
"include": [
"wwwroot",
"web.config"
]
},

"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}

I'm not going to break down everything in here, but I will explain the important parts. Also, if you're using NuGet in Visual Studios, a lot of this should be self-managing.

  • dependencies is where you put the libraries you'll be using. As you can see, there are only four to get started with MVC and what each one is for should be self-explanatory.
  • tools is for Visual Studio add-ons to help with development. If you're not using Visual Studio, you don't have to worry about this. Even if you are, you may not want to bother adding something here depending on how useful you find it.

  • frameworks is simply what .NET library you're working with. This will probably only be changed when you want to upgrade a project's .NET version.

  • Next is buildOptions, runtimeOptions, and publishOptions. This is where configuration options related to build and deployment go.

  • Then we have scripts. This is for commands you'd like to run at various steps of building and deploying. You can see the current value will deploy the compiled code to the IIS server.

  • And finally we have the web.config file:

web.config

xml
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
</system.webServer>
</configuration>

Before project.json everything was stored in an XML config file like this one. Unfortunately, some things are still in here for legacy's sake (such as project settings for IIS), but fortunately these are settings you'll probably never have to change. So I don't overwhelm you with information you probably won't need, I won't spend time on this file other than saying you'll likely need to include it as-is.

Conclusion

I hope that this is helpful for getting started with ASP.NET for those of you using Visual Studio Code over the traditional VS. Visual Studio for Mac was recently announced, so who knows if Linux may be next?

Let me know in the comments if you found this tutorial for VS Code helpful, and what you'd like to see more of regarding ASP.NET and OpenShift!

Check out the previous posts in this series: