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. routes.IgnoreRoute("{*allsvc}", new { allsvc = @".*\.svc(/.*)?" });
  10. routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  11. routes.MapRoute(
  12. name: "Default",
  13. url: "{controller}/{action}/{id}",
  14. defaults: new
  15. {
  16. controller = "Home",
  17. action = "Index",
  18. id = UrlParameter.Optional
  19. }
  20. );
  21. }
  22. }
  23. }
  • 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=neutral, PublicKeyToken=31BF3856AD364E35">
  8. <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection,
  9. System.Web.WebPages.Razor, Version=3.0.0.0,
  10. Culture=neutral, PublicKeyToken=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=neutral, PublicKeyToken=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=neutral, PublicKeyToken=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. <pages
  39. validateRequest="false"
  40. pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter,
  41. System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
  42. pageBaseType="System.Web.Mvc.ViewPage,
  43. System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
  44. userControlBaseType="System.Web.Mvc.ViewUserControl,
  45. System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  46. <controls>
  47. <add assembly="System.Web.Mvc, Version=5.0.0.0,
  48. Culture=neutral, PublicKeyToken=31BF3856AD364E35"
  49. namespace="System.Web.Mvc" tagPrefix="mvc" />
  50. </controls>
  51. </pages>
  52. </system.web>
  53. <system.webServer>
  54. <validation validateIntegratedModeConfiguration="false" />
  55. <handlers>
  56. <remove name="BlockViewHandler"/>
  57. <add name="BlockViewHandler" path="*" verb="*"
  58. preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
  59. </handlers>
  60. </system.webServer>
  61. </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.