Minimal ASP.NET MVC 6 Application

Posted by Anuraj on Friday, October 2, 2015

aspnet5 mvc6

Today in ASP.NET forums someone asked a question like a minimal MVC 6 application, where the controller method returns a simple string. For an ASP.NET 5 project, you require minimum two files, project.json, references required for the project and startup.cs, entry point and services configuration. Since it is MVC6 application you require one controller as well.

Here is the project.json file.

{
  "webroot": "wwwroot",
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.AspNet.Server.IIS": "1.0.0-*",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-*",
    "Microsoft.AspNet.Mvc": "6.0.0-*"
  },

  "commands": {
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5001"
  },

  "frameworks": {
    "dnx451": {}
  }
}

Here is the startup.cs file, which is the entry point to the application.

using Microsoft.AspNet.Builder;
using Microsoft.Framework.DependencyInjection;

namespace EmptyApplication
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseMvcWithDefaultRoute();
        }
    }
}

And finally the controller class.

using Microsoft.AspNet.Mvc;

namespace EmptyApplication
{
	public class HomeController : Controller
	{
		public IActionResult Index()
		{
			return Content("Hello World");
		} 
	}		
}

You can execute the dnu restore first, then execute the dnx web command.

Happy Programming.



Did you like this article? Share it with your friends

Facebook Twitter Google+ LinkedIn Reddit StumbleUpon



MVP
Subscribe


Copyright © 2017 - Anuraj P. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Hosted with ❤ by GitHub