Build and run your first beta4 ASP.NET application

UPDATE!! 2/8/2015

The kre home is being renamed again. I will update these posts that referred to the new .dotnet home for runtime as that appears to be going away.

 

 

Let’s build a very simple application using the latest beta4 nightly build packages and Visual Studio 2015 CTP.

Follow the previous post on installing CoreCLR beta4 on your machine: Setting up beta4 dotnet runtime on your machine

You can now go to your dotnet runtime bin directory and run the dotnetsdk.ps1 shell script from there. I might as well check for a new runtime version and lo and behold there is a new one already and its now the active one. Note I made the alias “default” by adding -a option.

withalias

 

Open a command window and go to your .dotnet directory. Run the temp-set-envvars.cmd file to set up your environment. This will set you up to use the dotnet runtime we had just updated.

setenvvars

Using Visual Studio 2015 CTP let’s create a simple MVC 6 and test it out on the new runtime.

I created a new ASP.NET Web Application and used the Empty template, selecting the ASP.NET 5 Empty template on the screen after this one.

beta4rockswebapp

 

 

I’ll update my project.json file and make dependencies look like this:

"dependencies": {
  "Microsoft.AspNet.Hosting": "1.0.0-beta4-10972",
  "Microsoft.AspNet.Diagnostics": "1.0.0-beta4-12017",
  "Microsoft.AspNet.Mvc": "6.0.0-beta4-12468",
  "Microsoft.AspNet.Server.WebListener": "1.0.0-beta4-11380",
  "Microsoft.Framework.ConfigurationModel.Json": "1.0.0-beta4-10750",
  "Microsoft.Framework.Logging.Console": "1.0.0-beta4-10759"
},
"frameworks": {
  // "aspnet50" : { },
  "aspnetcore50": { }
}

I’ve also commented out the full framework.

I’ll add a Controllers directory to the project and add an MVC Controller Class called HomeController to that directory.

homecontroller

 

Next I’ll add a Views directory with a Home sub-directory under that, then add an MVC View Page to the \Views\Home directory called Index.cshtml.

mvcview

 

Let’s add an ASP.NET Configuration file to our project also called Config.json.

configfile

 

I modified the Startup.cs file as follows:

using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Logging.Console;

namespace Beta4RocksWebApp
{
  public class Startup
  {
    public Startup(IHostingEnvironment env)
    {
    // Setup configuration sources.
    Configuration = new Configuration()
    .AddJsonFile("Config.json")
    .AddEnvironmentVariables();
  }

  public IConfiguration Configuration { get; set; }

  // This method gets called by the runtime.
  public void ConfigureServices(IServiceCollection services)
  {
    services.AddMvc();
  }
  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory)
  {
    // Configure the HTTP request pipeline.
    // Add the console logger.
    loggerfactory.AddConsole();

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

We are almost there, my index.cshtml file looks like this:

<html>
<head>
<title>TO DO List</title>
</head>
<body>
<div>
<h1>Glenn’s Dotnet Runtime Tracker</h1>
</div>
<div>
<table>
<thead>
<tr>
<th>Description</th>
<th>Wow Factor</th>
</tr>
</thead>
<tbody>
<tr><td>Beta 1</td><td>What?</td></tr>
<tr><td>Beta 2</td><td>Ummmm</td></tr>
<tr><td>Beta 3</td><td>Nice</td></tr>
<tr><td>Beta 4</td><td>Give me a few</td></tr>
<tr><td>RC 1</td><td>Not so fast</td></tr>
</tbody>
</table>
</div>
</body>
</html>

My solution tree looks like this:

solntree

Save and Compile the project. Make sure it compiles without errors.

Now go to the project home in the command shell we set up earlier, it’s where the project.json file is.

cmdshell

Run the following k command in the shell.

k --packages C:\Users\glenn\.kpm\packages Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000

If all is well you’ll see your app start like this:
startingapp

 

Open a web browser and enter http://localhost:5000
apprunning

 

 

How can I verify what runtime I’m using? If I open Resource Monitor application I should be able to find the dotnet.exe process running and I can.

resourcemonitor

 

I’ll be expanding on this example app in the near future.