Build your beta4 MVC 6 app from scratch using Code gen – Part 2

There is a newer updated blog for this here.

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.

 

 

Ok, let’s do what we set out to do, create an app from scratch using the tooling provided by ASP.NET 5.

Make sure you have looked at Part 1 of this series,  Part 1.

Starting from the command window we set up last time, type   k   gen.

thegencmd
We remember this gave us a few command possibilities, controller and view.
Let’s look at the help for the controller command.
controllercmdoptions

Let’s create a Home MVC 6 controller.
Type k  gen  controller  -name  Home  -scripts  -udl
We will see a lot happen to our once “empty” project.

runningkgen

Notice the comment at end of run to check the ScaffoldingReadMe.txt file. That tells us to add some things to our Startup class which we will do shortly.

A look at our project now shows a lot has been added.

newsolntree

We have new folders, javascript, etc, and most important a Controller class called HomeController. Two dependencies were also added for us to our project.json file.

 "Microsoft.AspNet.Mvc": "6.0.0-*",
 "Microsoft.AspNet.StaticFiles": "1.0.0-*"

Let’s add an Index view for our Home controller under Views\Home folder creating the Home sub-folder first. Let the view have this for content.

@{
ViewBag.Title = “Home Page”;
Layout = “~/Views/Shared/_Layout.cshtml”;
}
<h2>FromScratchWebApp – The Web made easy</h2>

 

Ok, one more step and we can run. Remember the file output for us, ScaffoldingReadMe.txt? It tells us exactly what to put in our Startup class.

Replace the contents of Configure method that we had added with what is in the file.

Add the using directives provided to the top of the file as well.

We are done. Build the app making sure there are no build errors.

Now run the Web Listener again by entering k  web in the command window we set up.
Now in a web browser browse to http://localhost:5000/
The web page with the auto generated controller looks like this
webpagewithcontroller

 

Not bad for doing almost no coding ourselves.

Next, we unleash the power of the k  ef commmand.
Stay tuned.