Learn How to configure Lightswitch with MVC Part 2

 Introduction
 
This article shows the walk through of how to use MVC with Visual Studio LightSwitch In previous article we have seen how to get started by creating a LightSwitch Application..Now lets continue by creating the MVC structure.
  • Step 1: Create the following folders in the Server project as shown
  1. App_Start
  2. Controllers
  3. Models
  4. Views
  
 
  •  Step 2 Add a class file named RouteConfig.cs to the App_Start folder and add the following implementation 
  1. using System.Web.Mvc;  
  2. using System.Web.Routing;  
  3. namespace LightSwitchApplication  
  4. {  
  5.     public class RouteConfig  
  6.     {  
  7.         public static void RegisterRoutes(RouteCollection routes)  
  8.         {  
  9.               
  10.             routes.IgnoreRoute("{*allsvc}"new { allsvc = @".*\.svc(/.*)?" });  
  11.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  12.             routes.MapRoute(  
  13.                 name: "Default",  
  14.                 url: "{controller}/{action}/{id}",  
  15.                 defaults: new  
  16.                 {  
  17.                     controller = "Home",  
  18.                     action = "Index",  
  19.                     id = UrlParameter.Optional  
  20.                 }  
  21.             );  
  22.         }  
  23.     }  
  24. }  
  • Step 3 Right click on views folder and add Web.config file and use the following code
 
 
 
  1. <?xml version="1.0"?>  
  2. <configuration>  
  3.   <configSections>  
  4.     <sectionGroup name="system.web.webPages.razor"   
  5.                   type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup,   
  6.                   System.Web.WebPages.Razor, Version=3.0.0.0,   
  7.                   Culture=neutralPublicKeyToken=31BF3856AD364E35">  
  8.       <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection,   
  9.                System.Web.WebPages.Razor, Version=3.0.0.0,   
  10.                Culture=neutralPublicKeyToken=31BF3856AD364E35"  
  11.                requirePermission="false" />  
  12.       <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection,   
  13.                System.Web.WebPages.Razor, Version=3.0.0.0,   
  14.                Culture=neutralPublicKeyToken=31BF3856AD364E35"  
  15.                requirePermission="false" />  
  16.     </sectionGroup>  
  17.   </configSections>  
  18.   <system.web.webPages.razor>  
  19.     <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory,   
  20.           System.Web.Mvc, Version=5.0.0.0,   
  21.           Culture=neutralPublicKeyToken=31BF3856AD364E35/>  
  22.     <pages pageBaseType="System.Web.Mvc.WebViewPage">  
  23.       <namespaces>  
  24.         <add namespace="System.Web.Mvc" />  
  25.         <add namespace="System.Web.Mvc.Ajax" />  
  26.         <add namespace="System.Web.Mvc.Html" />  
  27.         <add namespace="System.Web.Routing" />  
  28.       </namespaces>  
  29.     </pages>  
  30.   </system.web.webPages.razor>  
  31.   <appSettings>  
  32.     <add key="webpages:Enabled" value="false" />  
  33.   </appSettings>  
  34.   <system.web>  
  35.     <httpHandlers>  
  36.       <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>  
  37.     </httpHandlers>  
  38.       
  39.     <pages  
  40.         validateRequest="false"  
  41.         pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter,   
  42.         System.Web.Mvc, Version=5.0.0.0, Culture=neutralPublicKeyToken=31BF3856AD364E35"  
  43.         pageBaseType="System.Web.Mvc.ViewPage,   
  44.         System.Web.Mvc, Version=5.0.0.0, Culture=neutralPublicKeyToken=31BF3856AD364E35"  
  45.         userControlBaseType="System.Web.Mvc.ViewUserControl,   
  46.         System.Web.Mvc, Version=5.0.0.0, Culture=neutralPublicKeyToken=31BF3856AD364E35">  
  47.       <controls>  
  48.         <add assembly="System.Web.Mvc, Version=5.0.0.0,   
  49.              Culture=neutralPublicKeyToken=31BF3856AD364E35"  
  50.              namespace="System.Web.Mvc" tagPrefix="mvc" />  
  51.       </controls>  
  52.     </pages>  
  53.   </system.web>  
  54.   <system.webServer>  
  55.     <validation validateIntegratedModeConfiguration="false" />  
  56.     <handlers>  
  57.       <remove name="BlockViewHandler"/>  
  58.       <add name="BlockViewHandler" path="*" verb="*"  
  59.            preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />  
  60.     </handlers>  
  61.   </system.webServer>  
  62. </configuration>  
 
 Summary

In this small article, I explained how to prepare LightSwitch Application to use MVC, In next article we will see the complete configuration.