Please enable Javascript for better experience...
Setting Web.Config file for MVC project
By Rahul Kumar Jha | Mar 2, 2015 | In Tutorials | Total Views [ 29701 ]
Taged In
(15 Like)

In previous part of tutorial you saw how to register Bundle, Route and Filters in Global.asax file. We are just one step behind setting up MVC project. In this part of tutorial you will see setting up necessary configurations in web.config file in application.


In MVC project you will find two web.config files - One in root path and another in Views folder.  Let's see one by one about these files with its uses in MVC application.

Adding Web.config files

Web.config file at root path will serve similar as in other project type. It contains all the necessary configuration details like Connection, Authentication etc. Web.config file in Views folder contains setting related to MVC type like RazorFactory, HandlersPage Directives etc.

Adding web.config file at root folder

To add web.config file in your project follow below steps.

1) Right Click folder > Click Add > Add Item..

2) Select Web tab > Web Configuration File > Click Add

Put this code to web.config file.

<?xml version="1.0" encoding="utf-8"?>
  For more information on how to configure your ASP.NET application, please visit
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="" newVersion="" />

Adding web.config file in Views folder

Add web.config file in Views folder as above and copy below code to the file.

<?xml version="1.0"?>

    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />

    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />

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

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

You will find a Page tag where you can add assembly references to  be used by views. Now try to run your project and you are done.

You have created your MVC project.

<< Register Bundle, Route and Filters in Global.asax file in MVC

Share this

About the Author

Rahul Kumar Jha
Rahul Kumar Jha
Founder, Developer

Public profile: user/profile/99900001

Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, ANGULAR, ANGULAR JS, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

User's Comments

Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID