Pages

Thursday, September 29, 2016

MVC Setup from Empty ASP.NET template

ASP.Net Empty template doesn't have any nuget packages included, so no packages.json will exist.

1. Installing MVC

Packages Icluded

Microsoft.AspNet.Mvc

Microsoft.AspNet.WebPages

Microsoft.AspNet.Razor

Microsoft.Web.Infrastructure   

References Installed

System.Web.WebPages.Razor

System.Web.WebPages.Deployment

System.Web.WebPages

System.Web.Razor

System.Web.Mvc

System.Web.Helpers

Microsoft.Web.Infrastruture

2. Uninstall-package - References Removed

Microsoft.AspNet.Mvc

System.Web.Mvc

Microsoft.AspNet.WebPages

System.Web.WebPages.Deployment

System.Web.WebPages

System.Web.Helpers

Microsoft.AspNet.Razor

System.Web.Razor

Microsoft.Web.Infrastructure

Microsoft.Web.Infrastruture

Finial complete MVC directory structure:

3. Add Global Application Class File to application

Global.asax.cs

Project right click > Add > New Item… > Select Global Application Class

4. Create folders

·         App_Start (can named anything)

·         Controllers

·         Views

5. Under App_Start create a class file and name it whatever you want - RouteConfiguration.

.axd file types are HTTP Handlers, which should be ignored.

Add code to file:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Routing;

 

namespace myWebsite.App_Start

{

    public static class RouteConfiguration

    {

        public static void Register(RouteCollection routes)

        {

            routes.IgnoreRoute("{resource}.axd/{*pathinfo}");

 

            routes.MapRoute(

                "Default",

                "{controller}/{action}/{id}",

                new { controller = "Home", action = "Index"}

                );

        }

 

    }

}

 

6. Add code to Global.asax class file passing all routes.

using com.waggintails.App_Start;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Routing;

using System.Web.Security;

using System.Web.SessionState;

 

namespace com.waggintails

{

    public class Global : System.Web.HttpApplication

    {

 

        protected void Application_Start(object sender, EventArgs e)

        {

            RouteConfiguration.Register(RouteTable.Routes);

        }

    }

}

7. Within controllers folder, add a MVC5 Controller - Empty

Rename to whatever you like – Home

Add code to file:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace com.waggintails.Controllers

{

    public class HomeController : Controller

    {

        // GET: Default

        public ActionResult Index()

        {

            return View();

        }

    }

}

 

8. Within views folder, add an empty view and name the view as the same name that you had for the controller.

Add code to file:

@{

    ViewBag.Title = "Home";

}

 

<h2>Home</h2>

 

And within Views folder, add a web.config file.

Add code to file:

<?xml version="1.0"?>

 

<configuration>

    <configSections>

        <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">

            <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />

            <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />

        </sectionGroup>

    </configSections>

 

    <system.web.webPages.razor>

        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

        <pages pageBaseType="System.Web.Mvc.WebViewPage">

            <namespaces>

                <add namespace="System.Web.Mvc" />

                <add namespace="System.Web.Mvc.Ajax" />

                <add namespace="System.Web.Mvc.Html" />

                <add namespace="System.Web.Routing" />

                <add namespace="com.waggintails" />

            </namespaces>

        </pages>

    </system.web.webPages.razor>

 

    <appSettings>

        <add key="webpages:Enabled" value="false" />

    </appSettings>

 

    <system.webServer>

        <handlers>

            <remove name="BlockViewHandler"/>

            <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />

        </handlers>

    </system.webServer>

 

    <system.web>

        <compilation>

            <assemblies>

                <add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

            </assemblies>

        </compilation>

    </system.web>

</configuration>

Press F5 and Home view should show correctly

9. <Optional> Include a layout view

Within the Views directory add a view called

_ViewStart.cshtml

This view allow you to specify the path for the layout view.

Add code:

@{

    Layout = "~/Views/Shared/_Layout.cshtml";

}

Within the directory you had specified in _ViewStart, add the specified view -  _Layout.cshtml

Add code – when using layouts, must include @RenderBody.

<!DOCTYPE html>

 

<html>

<head>

    <meta name="viewport" content="width=device-width" />

    <title>_Layout</title>

</head>

<body>

    <div>

        @RenderBody()

    </div>

</body>

</html>

With layout, finial complete MVC directory structure:




This post is for the purpose of my notes only and sometimes a rant.

“I invented nothing new. I simply assembled the discoveries of other men behind whom were centuries of work. Had I worked fifty or ten or even five years before, I would have failed. So it is with every new thing. Progress happens when all the factors that make for it are ready and then it is inevitable. To teach that a comparatively few men are responsible for the greatest forward steps of mankind is the worst sort of nonsense.”
Henry Ford