Showing posts with label NuGet. Show all posts
Showing posts with label NuGet. Show all posts

Saturday, June 18, 2016

ASP.NET MVC 5 From Scratch : Add KnockoutJs to Project With NuGet

In this blog we will add the KnockoutJs JavaScript library to our MvcApp project.  KnockoutJs is a great lightweight data binding library.  Follow the steps below to add KnockoutJs to your ASP.NET MVC project.

Step-By-Step Instructions:

1.  Right-click on the project's "References" node, then select "Manage NuGet Packages"



2. In right hand side type in "knockoutjs", then click on the "Install" button next to the knockoutjs package


3.  You should see a green checkmark after you finished adding the knockoutjs library to the MvcApp project.



4. In the "Scripts" folder you will see the knockout JavaScript files


5.  Now that we have the knockoutjs files we need to add the files to the BundleConfig.cs file to register them

6.  Open the BundleConfig.cs file in the "App_Start" folder type the following lines of code inside the RegisterBundles method, this tells MVC to include any files in the "Scripts" folder that starts with the string "knockout"

            bundles.Add(new ScriptBundle("~/bundles/knockout").Include(
"~/Scripts/knockout*"));

BundleTable.EnableOptimizations = false;
bundles.UseCdn = true;

The entire code looks like the following up to this point in our project.

using System.Web;
using System.Web.Optimization;

namespace MvcApp
{
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery", "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js").Include("~/Scripts/jquery-{version}.js"));

bundles.Add(new ScriptBundle("~/bundles/bootstrap","https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js").Include("~/Scripts/bootstrap.js","~/Scripts/respond.js"));

bundles.Add(new ScriptBundle("~/bundles/knockout").Include(
"~/Scripts/knockout*"));

bundles.Add(new StyleBundle("~/Content/bootstrap", "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css").Include("~/Content/bootstrap.css"));

bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/Styles.css"));

BundleTable.EnableOptimizations = false;
bundles.UseCdn = true;
}
}
}

7. In the "Views" → "Shared" folder, open the "_Layout.cshtml" file and add the following code



        @RenderBody()
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/knockout")


If you press Ctrl+F5 to run the application and look at the browser source code you will see that the knockout js files have been added to the page
 


ASP.NET MVC 5 From Scratch : Adding BundleConfig

In our previous blog we've added a _ViewStart.cshtml layout to our project, which is the default layout for our pages if no layout is specified for the page.  In this blog we will add BundleConfig for the JavaScript libraries which includes JQuery, and Bootstrap that we've added to our MvcApp project in the previous blogs.  A configuration bundle allows you to group files that belongs in the same libraries together so that they can called with just one line of code.  In this blog we will be creating configuration bundles for JQuery, and Bootstrap to the MvcApp project.

Step-By-Step Instructions:

1.  Right-click on the folder "App_Start", then select Add → New Item → Visual C# → Class, name the class BundleConfig.cs

























2.  Now the App_Start folder should look like the screenshot below










3.  Open the BundleConfig.cs file, then delete the existing using statements, and then add the following namespaces

using System.Web;
using System.Web.Optimization;

4. The resulting code should look like the following up to this point
using System.Web;
using System.Web.Optimization;

namespace MvcApp.App_Start
{
public class BundleConfig
{
}
}

5. If you see the System.Web.Optimization with the red underline, that means you have the reference to your project













6.  To add the reference open the NuGet Package Manager Console by selecting Tools → NuGet Package Manager →  Package Manager Console


7.  Once the Package Manager Console has been opened type in the following command Install-Package Microsoft.AspNet.Web.Optimization, then press "Enter"













8.  Once the package has been add you should get the following message "Successfully added 'Microsoft.AspNet.Web.Optimization 1.1.3' to MvcApp."

10.  Now the red underline is gone from the BundleConfig.cs file
















11. Inside the BundleConfig class add a static method call RegisterBundles with a BundleCollection call bundles parameter.  So the code should look like the following

using System.Web;
using System.Web.Optimization;

namespace MvcApp.App_Start
{
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
}
}
}

The code above tells MVC to register the bundles in the static method RegisterBundles

12. First lets create a bundle for the JQuery library by adding the following lines of code to the RegisterBundles method
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));

The code above tells MVC to include all the files in the "Scripts" folder that has the string "jquery" in the file followed by a "-" and version number with the file extension ".js". Also give it the reference of "~/bundles/jquery", this is how we are going to reference the bundle in our views later on.

13. Now we are going to add the add the Boostrap library to the project in a similar fashion.
            bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js",
"~/Scripts/respond.js"));

bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css"));

The code above tells MVC to reference the bootstrap script bundle as "~/bundles/bootstrap" and the include the files "bootstrap.js" and "respond.js" in the "Scripts" folder. Likewise reference the css files for bootstrap as "~/Content/css" and include the files "bootstrap.css" and "site.css" in the "Content" folder. The css bundle is a catch all bundle where all the css files in the first level of the Content folder will be referenced.

The final code for the BundleConfig class should look like this

using System.Web;
using System.Web.Optimization;

namespace MvcApp.App_Start
{
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));

bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js",
"~/Scripts/respond.js"));

bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css"));
}
}
}

14. Now it's time to add the bundle configurations to our _Layout.csthml view.

15.  Open the _Layout.cshtml file under Views → Shared folder

16.  First we will add the css bundle, by typing in the following code in the head section
 @Styles.Render("~/Content/css")

17. Now scroll to the bottom of the page and add the JavaScript bundles to the layout page
    @Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")

The completed layout code should look like the following
<!DOCTYPE html>
<html>
<head>
<title>@Page.Title</title>
@RenderSection("head", required: false)
@Styles.Render("~/Content/css")
</head>
<body>
<h1>This is from the _ViewStart.cshtml</h1>
@RenderBody()
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
</body>
</html>

If Visual Studio is complaining about the "@Scripts" and "@Styles" reference is missing then add the System.Web.Optimization to the Views web.config file.





















18.  Open the web.config file under the folder Views and add the following code
<add namespace="System.Web.Optimization">
</add>

Inside the <namespaces> tag
Here is how the web.config file should look like

  <system .web.webpages.razor="">
<host factorytype="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.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.Optimization">
<add namespace="System.Web.Routing">
<add namespace="WebApplication1">
</add></add></add></add></add></add></namespaces>
</pages>
</host></system>

If you build the MvcApp project the missing reference error will disappear for the @Scripts and @Styles namespaces.

 19. Press Ctrl+F5 to run the application, you will get the following error message

Server Error in '/' Application.
Could not load file or assembly 'Newtonsoft.Json' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.

20.  To get rid of the error you have to open the web.config file under the application's root folder and delete the following lines

      
<assemblyidentity culture="neutral" name="Newtonsoft.Json" publickeytoken="30ad4fe6b2a6aeed">
<bindingredirect newversion="6.0.0.0" oldversion="0.0.0.0-6.0.0.0">
</bindingredirect></assemblyidentity></dependentassembly>

21. Now open the NuGet Package Manager Console and type in the following update-package Newtonsoft.Json -reinstall, then press enter

22. When the package Newtonsoft.Json is reinstalled again you will get the following message "Successfully added 'Newtonsoft.Json 5.0.4' to MvcApp."

23.  There is one more thing that you have to do to make the bundle configs work, you have to register the bundles at the Application_Start() method in the Global.asax.cs file.

24. Open the Global.asax.cs add the following line at the end of the Application_Start() method

BundleConfig.RegisterBundles(BundleTable.Bundles);

Your Global.asax.cs file should look like the following at this point
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace MvcApp
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}


25. Now run the application again by pressing Ctrl + F5 and you should see the following
















26.  If you look at the source code you will see that JQuery and Bootstrap files have been added to the View that is displayed on the browser

<!DOCTYPE html>
<html>
<head>
<title></title>

<link href="/Content/bootstrap.css" rel="stylesheet"/>

</head>
<body>
<h1>This is from the _ViewStart.cshtml</h1>


<h3>This is from Index.cshtml</h3>
<script src="/Scripts/jquery-2.1.4.js"></script>

<script src="/Scripts/bootstrap.js"></script>


ASP.NET MVC 5 From Scratch : Add JQuery Library Using NuGet

In the previous blog we've created an empty ASP.NET MVC 5 project.  It's a great starting point but, it's missing a lot things that we will need later.  In this blog we will add JQuery to the empty ASP.NET MVC 5 project that we've just created.

Step-By-Step Instructions:

1. Open the empty ASP.NET MVC 5 project that you've just created

2.  Right click on the the "References" node in the "Solution Explorer", then select "Manage NuGet Packages"















3. In the search box on the right of NuGet window type "JQuery", the search result will be displayed











4. Click on the "Install" button next to the JQuery result, it should be at the top of the list











5. Once the JQuery library has been installed a green check mark is displayed, click on the "Close" button












6.  Now you should see the JQuery scripts in the "Scripts" folder








7.  To use the JQuery .js script reference the jquery-2.14.min.js file with the following code between the <head> element
<script src="~/Scripts/jquery-2.1.4.min.js"></script>

8. You can use the following code to test if JQuery is working in your ASP.NET MVC project
    <script>
$(document).ready(alert("jquery is working"));
</script>

9. So you can modify the "Index.cshtml" to have the following markup to test if JQuery is working or not, if JQuery is working you should get an alert box saying "jquery is working"
@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/jquery-2.1.4.min.js"></script>
</head>
<body>
<div>
Hello World!
</div>
<script>
$(document).ready(alert("jquery is working"));
</script>

</body>
</html>


ASP.NET MVC 5 From Scratch : Add Bootstrap Library Using NuGet

In the previous blog we've created added the JQuery library to an empty ASP.NET MVC 5 project. In this blog we will add the Bootstrap to the empty ASP.NET MVC 5 project that we've just created.

Step by Step Instructions:

1. Open the empty ASP.NET MVC 5 project that you've just created

2.  Right click on the the "References" node in the "Solution Explorer", then select "Manage NuGet Packages"















3.  In the search box on the right of NuGet window type "bootstrap", the search result will be displayed

4. Click on the "Install" button next to the "Bootstrap" result, it should be at the top of the list

5.  Once the Bootstrap library has been installed a green check mark is displayed, click on the "Close" button

6.  Now you should see the Bootstrap scripts in the "Scripts" folder

7.  You will also noticed that the "Contents" folder has been created, this folder was created when NuGet installed the Bootstrap library in the "MvcApp" project. This folder contains the cascading stylesheet that Bootstrap uses


























8.  Now let's test our bootstrap installation, in the "Index.cshtml" type in the following markup

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery-2.1.4.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
</head>
<body>
<div class="jumbotron">
Hello Bootstrap!
<p><a class="btn btn-primary btn-lg" href="#" role="button">Installed</a></p>
</div>

</body>
</html>

9. When you press Ctrl+F5 to run the application you should see the following in your browser

Monday, March 23, 2015

ASP.NET MVC : Upgrade ASP.NET MVC 4 to ASP.NET MVC 5 Using NuGet Part 1















In this blog we will go over how to upgrade your existing project which uses ASP.NET MVC 4 to ASP.NET MVC 5 using NuGet.  During the upgrade you will encounter some errors but they are easy enough to fix by changing the Web.Config file.  In the first blog we will create a simple ASP.NET MVC 4 application from scratch.

But first thing is first let's begin by creating a project in ASP.NET MVC 4:

  1. Create a blank solution in Visual Studio call "MVCProjects"
Visual Studio 2013 Create New Blank Solution

2.  Now add a new project to the "MVCProjects" by selecting ASP.NET MVC4, call it "MyMVC4".
Make sure you select Visual C# → Web → Visual Studio 2012 → ASP.NET MVC 4 Web Application, then click "OK"

 ASP.NET MVC 4 Web Application
























3. Select the "Empty" template, and "Razor" as the "View Engine", then click "OK"

Razor View Engine

4. Right click on the "Controller" and select "Add" → "Controller"

Razor View Engine

5.  On the "Add Controller" screen type "HomeController" on the "Controller name" text box and select "Empty MVC controller".  We just want to keep things simple for this blog.  Then click "Add"

Empty MVC Controller



















6.  You see the "HomeController.cs" class being added to the "Controllers" folder

Home Controller

7.  Double click on the "HomeController.cs" file and you will see the following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyMVC4.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/

public ActionResult Index()
{
return View();
}

}
}

8.  Right click on the "Index()" method and select "Add View"

MVC Add View







9.   Uncheck the "Use a layout or master page" checkbox. Accept the default settings, MVC uses conventions so the view has the same name as the method name in the "HomeController.cs" file which is "Index()", then click "Add"


Index View Configuration
























10.  In the "Views" folder you see that the "Home" folder has been created with the view "Index.cshtml", the .cshtml extension means that the view uses the C# syntax.

11.  Double click on the view "Index.cshtml" and type "Hello World" between the <div> tag like the markup below

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
Hello World!
</div>
</body>
</html>

12. Press Ctrl+F5 to run the application, you will see the following.

MVC Browse Index View













In this blog we went over how to create a simple ASP.NET MVC 4 application. In the next part we will go through the steps on how to upgrade your ASP.NET MVC 4 application to ASP.NET MVC 5 using NuGet

ASP.NET MVC : Upgrade ASP.NET MVC 4 to ASP.NET MVC 5 Using NuGet Part 1

In this blog we will go over how to upgrade your existing project which uses ASP.NET MVC 4 to ASP.NET MVC 5 using NuGet.  During the upgrade you will encounter some errors but they are easy enough to fix by changing the Web.Config file.  In the first blog we will create a simple ASP.NET MVC 4 application from scratch.

But first thing is first let's begin by creating a project in ASP.NET MVC 4:

  1. Create a blank solution in Visual Studio call "MVCProjects"
Visual Studio 2013 Create New Blank Solution

2.  Now add a new project to the "MVCProjects" by selecting ASP.NET MVC4, call it "MyMVC4".
Make sure you select Visual C# → Web → Visual Studio 2012 → ASP.NET MVC 4 Web Application, then click "OK"

 ASP.NET MVC 4 Web Application
























3. Select the "Empty" template, and "Razor" as the "View Engine", then click "OK"

Razor View Engine

4. Right click on the "Controller" and select "Add" → "Controller"

Razor View Engine

5.  On the "Add Controller" screen type "HomeController" on the "Controller name" text box and select "Empty MVC controller".  We just want to keep things simple for this blog.  Then click "Add"

Empty MVC Controller



















6.  You see the "HomeController.cs" class being added to the "Controllers" folder

Home Controller

7.  Double click on the "HomeController.cs" file and you will see the following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyMVC4.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/

public ActionResult Index()
{
return View();
}

}
}

8.  Right click on the "Index()" method and select "Add View"

MVC Add View







9.   Uncheck the "Use a layout or master page" checkbox. Accept the default settings, MVC uses conventions so the view has the same name as the method name in the "HomeController.cs" file which is "Index()", then click "Add"


Index View Configuration
























10.  In the "Views" folder you see that the "Home" folder has been created with the view "Index.cshtml", the .cshtml extension means that the view uses the C# syntax.

11.  Double click on the view "Index.cshtml" and type "Hello World" between the <div> tag like the markup below

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
Hello World!
</div>
</body>
</html>

12. Press Ctrl+F5 to run the application, you will see the following.

MVC Browse Index View













In this blog we went over how to create a simple ASP.NET MVC 4 application. In the next part we will go through the steps on how to upgrade your ASP.NET MVC 4 application to ASP.NET MVC 5 using NuGet

ASP.NET MVC : Upgrade ASP.NET MVC 4 to ASP.NET MVC 5 Using NuGet Part 2

In the previous blog we went over how to create a simple ASP.NET MVC 4 application.  In this blog we will go over the steps to upgrade our ASP.NET MVC 4 application to ASP.NET MVC 5 application.

Step-By-Step Instructions:

  1. Open the "MyMVC4" project that you've created on the last blog
  2. Right click on the project and select "Manage NuGet Packages..."
Manage NuGet Packages











3.  Select Microsoft ASP.NET MVC and click "Install", as you can see the latest version is 5.2.3

NuGet ASP.NET MVC Version 5.2.3











4.  Click "I Accept" on the license agreement screen

APS.NET MVC  License Acceptance















5.  There will be a check mark after ASP.NET MVC 5 has been installed by NuGet













6.  Before you click the "Close" button make a note of the "Description" section, pay close attention to the "Dependencies" section, it will come in handy later.

Microsoft.AspNet.Mvc Description
















7.  Visual Studio will give you a message that to complete the upgrade you have to restart Visual Studio.  Click "OK"

Microsoft Visual Studio MVC Restart






8.  Close Visual Studio and then open the "MyMVC4" project again.
9.  Press "Ctrl+F5"
10.  Instead of seeing the friendly greeting of "Hello World!" you will be greeted with a nasty error message


Server Error in '/' Application.

[A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to [B]System.Web.WebPages.Razor.Configuration.HostSection. Type A originates from 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'. Type B originates from 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Users\TechJunkie\AppData\Local\Temp\Temporary ASP.NET Files\vs\12822f34\c6cc652d\assembly\dl3\23bbbc5e\1a551bd8_e563d001\System.Web.WebPages.Razor.dll'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: [A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to [B]System.Web.WebPages.Razor.Configuration.HostSection. Type A originates from 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'. Type B originates from 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Users\TechJunkie\AppData\Local\Temp\Temporary ASP.NET Files\vs\12822f34\c6cc652d\assembly\dl3\23bbbc5e\1a551bd8_e563d001\System.Web.WebPages.Razor.dll'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[InvalidCastException: [A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to [B]System.Web.WebPages.Razor.Configuration.HostSection. Type A originates from 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'. Type B originates from 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Users\TechJunkie\AppData\Local\Temp\Temporary ASP.NET Files\vs\12822f34\c6cc652d\assembly\dl3\23bbbc5e\1a551bd8_e563d001\System.Web.WebPages.Razor.dll'.]
System.Web.WebPages.Razor.WebRazorHostFactory.GetRazorSection(String virtualPath) +79
System.Web.WebPages.Razor.WebRazorHostFactory.CreateHostFromConfig(String virtualPath, String physicalPath) +44
System.Web.WebPages.Razor.RazorBuildProvider.GetHostFromConfig() +20
System.Web.WebPages.Razor.RazorBuildProvider.CreateHost() +17
System.Web.WebPages.Razor.RazorBuildProvider.EnsureGeneratedCode() +60
System.Web.WebPages.Razor.RazorBuildProvider.get_CodeCompilerType() +32
System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider(BuildProvider buildProvider) +59
System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders() +209
System.Web.Compilation.BuildProvidersCompiler.PerformBuild() +30
System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath) +9971917
System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +299
System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +103
System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound) +165
System.Web.Compilation.BuildManager.GetCompiledType(VirtualPath virtualPath) +10
System.Web.Compilation.BuildManager.GetCompiledType(String virtualPath) +28
System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.GetCompiledType(String virtualPath) +6
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +54
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +173
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +28
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651116
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155



Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34212              

11.  Remember the ASP.NET MVC 5 Description in NuGet?  It says
"Dependencies

        Microsoft.AspNet.WebPages (≥ 3.2.3 && < 3.3.0)

        Microsoft.AspNet.Razor (≥ 3.2.3 && < 3.3.0)
"
and 

"
Id: Microsoft.AspNet.Mvc
Version: 5.2.3
"

12.  Even though we've already installed ASP.NET MVC 5 on our ASP.NET MVC 4 project.  The Web.Config file still refers to the old ASP.NET MVC 4 version.  NuGet did not change the Web.Config file for us in the "Views" folder.  Although it did change the Web.Config in the root application folder.

  <runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>

13.If you look the Web.Config file under the "Views" folder you will see that the Web.Config file still references the old version of MVC and Razor.
<?xml version="1.0"?>

<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.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=4.0.0.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" />
</namespaces>
</pages>
</system.web.webPages.razor>

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

<system.web>
<httpHandlers>
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>

<!--
Enabling request validation in view pages would cause validation to occur
after the input has already been processed by the controller. By default
MVC performs request validation before a controller processes the input.
To change this behavior apply the ValidateInputAttribute to a
controller or action.
-->
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>

<system.webServer>
<validation validateIntegratedModeConfiguration="false" />

<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>



14. Change the Web.config file to use ASP.NET MVC 5, the Web.config should be changed to the following in order for you to get rid of the error.
<?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" />
</namespaces>
</pages>
</system.web.webPages.razor>

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

<system.web>
<httpHandlers>
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>

<!--
Enabling request validation in view pages would cause validation to occur
after the input has already been processed by the controller. By default
MVC performs request validation before a controller processes the input.
To change this behavior apply the ValidateInputAttribute to a
controller or action.
-->
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>

<system.webServer>
<validation validateIntegratedModeConfiguration="false" />

<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>

15. Press "Ctrl+F5" and you should see "Hello World!" again instead of the error.

ASP.NET MVC Hello World








There you have that is how you upgrade from ASP.NET MVC 4 to ASP.NET MVC 5.  It's not as straight forward, but it's not too difficult.


16.  Now that we have a working empty ASP.NET MVC 5 project, we need add the items to the projects manually.  You might have noticed that we are missing the "Scripts" folder.  What we want to do is add the "Scripts" folder manually from an existing ASP.NET MVC 5 regular project.  I usually zip up the "Scripts" folder and added it to the empty ASP.NET MVC 5 project.  Here is the "Scripts" folder zipped up.

Scripts.zip

  17. To add the "Scripts" folder to the MvcApp application that we've just created, download the Scripts.zip file then extract it.  Make sure you choose the option to "Extract here.." on your unzip utility program.

You have the following folders, make sure that when you extracted the .zip file that "Scripts" is the root folder and there's only one level to the folder.  If you select "Extract to Scripts\" option on the unzip utility you will get two nested "Scripts" folder, which is something we don't want.




18.  Now drag the extracted "Scripts" folder to Visual Studio at the top level of the MvcApp folder
















As you can see now we have the "Scripts" folder in our empty project, which is not so empty anymore.
    Blogs in this series:


    1. ASP.NET MVC : Upgrade ASP.NET MVC 4 to ASP.NET MVC 5 Using NuGet Part 1
    2. ASP.NET MVC : Upgrade ASP.NET MVC 4 to ASP.NET MVC 5 Using NuGet Part 2