Jury-rig


Software Engineering, a personal journey

.Net Core MVC, you shall not defeat me

I wanted to make a video but after the fifth attempt I give up and obviously once I stopped recording everything works great

Hence I've stuck the project on github and outlined the main gotcha's below.
First off I've used Jetbrains Rider to create a .Net Core Empty Web template which has the least about of crap in it and a good start to add basic MVC functionality. At the moment Core is using json to configure the project with the meat of the config going into the project.json:
{
    "dependencies": {
        "Microsoft.NETCore.App": {
            "version": "1.1.0",
            "type": "platform"
        },
        "Microsoft.AspNetCore.Diagnostics": "1.1.0",
        "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
        "Microsoft.Extensions.Logging.Console": "1.1.0",
        "Microsoft.AspNetCore.Mvc": "1.1.1",
        "Microsoft.Extensions.Configuration.FileExtensions": "1.1.0",
        "Microsoft.Extensions.Configuration.Json": "1.1.0",
        "Microsoft.AspNetCore.StaticFiles": "1.1.0"
    },

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

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

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

    "publishOptions": {
        "include": [
            "wwwroot",
            "**/*.cshtml"
        ]
    }
}
​
The project.json has a linked file name project.locked.json DO NOT mess with it. The project.json has the projects dependencies (NuGet stuff) and if you update your dependencies the project needs to be restored so that the project.locked.json and global.json can be updated. Additionally, one other gotcha is the publishOptions which help when you're publishing the final project and what additional resources need to be included. I wanted to add my core project to Docker but found that nothing loaded when I mounted the Docker container and pointed my browser at the project. This was because I hadn't included all my cshtml views in the published project.
Next up is the start.cs where the setup for MVC goes and is called in the main method of the project.
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace EmptyWeb
{
    public class Startup
    {
        public IConfigurationRoot Configuration { get; }

        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddEnvironmentVariables();

            Configuration = builder.Build();
        }

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

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();
            app.UseStaticFiles();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}"
                );
            });
        }
    }
}​
This is fie needs load of explaining as it's where the configuration happens but I'm tired so this will have to do.
Now for the main program file which like all C# console apps has the main method where everything goes to run the app.
using System.IO;
using Microsoft.AspNetCore.Hosting;

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

            host.Run();
        }
    }
}​

Well this would have been loads better with a view, I'll try again and post if I'm successful but here's the github:
https://github.com/juryrigcoder/DotNetCoreMVC.git
 


Comments



Have your say

Author:

Comment here (140 chars only)*:

Email address: