Thursday, October 15, 2015

Applying MVC 5 on Visual Studio 2012 Express

Visual Studio 2013 has been shipped with MVC 5 along with .Net 4.5.1 2 years ago. It even included a free communication version with full functionality. Since then, it has evolved with 5 updated versions until  July 2015.

Visual Studio 2013 combines Nuget 2.7, ASP.Net MVC 5, ASP.Net Web API 2, Entity Framework 6, ASP.Net Razor, ASP.Net Scaffolding, a new code generating framework for ASP.Net web applications, and Bootstrap 3, a layout and theme framework from twitter to provide responsive design through CSS 3, etc. Such new features are exciting for all ASP.Net web developers. However, for Microsoft Database or BI developers, if they are stick to SQL Server 2012, they would better to keep their old Visual Studio 2012 as the support for BI development such as SSAS/SSRS/SSIS 2012 on Visual Studio 2013 are not perfect.

Fortunately, Microsoft announced MVC 5 for visual studio 2012. Also, Visual Studio 2012 Update 4 and ASP.Net and Web Tools 2013.1 for Visual Studio  2012 would be also required, especially for Visual Studio 2012 Express for Web.

After you installed the web tools 2013.1 for Visual Studio 2012, your Visual Studio 2012 for Web would show following new ASP.Net web templates:

Note you still can't find a fully functional ASP.Net MVC 5 Web Application template, except an empty one.

You can create a new project using ASP.Net MVC 5 Empty Project, and then right click on the project, click Add.. > New Scaffolded Item..., select MVC on the left, then MVC 5 Dependencies and choose Full dependencies from the prompt.

This will add a default layout Views\Shared\_Layout.cshtml, Content folder, Scripts folder with bootsrap, jquery, modernizer, etc, and other things the Web Application template will create.

You could get the error "CS0103: The name 'Styles' doesnot exist in the current context", just add <namespace="System.Web.Optimization"/>  under <namespaces> in Views/web.config.

To add a fully functional ASP.Net MVC 5 Web Application template, download the MVC 5 template from here. Copy the file into the following folder of your PC:
%USERPROFILE%\Documents\Visual Studio 2012\Templates\ProjectTemplates\Visual C#\Web
and then change DefaultConnection in Web.config with following format:
<connectionStrings>
    <add name="MyDBConnection" connectionString="Data Source=localhost;Initial Catalog=MyDB;
        Persist Security Info=True;User ID=MyName;Password=MyPassword"
    providerName="System.Data.SqlClient"/>
</connectionStrings>
Now you can restart you Visual Studio 2012 express for Web and enjoy the new MVC 5 template.

If during the debug, you get the error "CS0234: The type or namespace name 'Ajax' does not exist in the namespace 'System.Web.Mvc' (are you missing an assembly reference?)", simply open the reference directory of your project under solution explorer, right click on the package name, choose the property, and change the Copy Local to true.

Note after you created your MVC 5 application, if you open your web.config file, you could find following issue:

It was said "The entityframework element has invalid child element providers.". This could give warning message for your project. To resolve this warning, you need to copy following 2 files:
to folder: "C:\Program Files\Microsoft Visual Studio 11.0\Xml\Schemas":

1. EntityFrameworkCatalog.xml
<?xml version="1.0" encoding="utf-8"?>
<SchemaCatalog xmlns="http://schemas.microsoft.com/xsd/catalog">
    <Association extension="config" schema="%InstallRoot%/xml/schemas/EntityFrameworkConfig_6_1_4.xsd" />
</SchemaCatalog>
2. EntityFrameworkConfig_6_1_4.xsd
<?xml version="1.0" encoding="utf-8"?>

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="entityFramework">
    <xs:complexType>
      <xs:all>
        <xs:element name="defaultConnectionFactory" type="ElementWithTypeAndParameters_Type" minOccurs="0" maxOccurs="1" />
        <xs:element name="providers" type="ProviderList_Type" minOccurs="0" maxOccurs="1" />
        <xs:element name="contexts" type="ContextList_Type" minOccurs="0" maxOccurs="1" />
        <xs:element name="interceptors" type="InterceptorList_Type" minOccurs="0" maxOccurs="1" />
      </xs:all>
      <xs:attribute name="codeConfigurationType" type="NonEmptyString_Type" use="optional" />
      <xs:anyAttribute namespace="http://schemas.microsoft.com/XML-Document-Transform" processContents="strict"/>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="ProviderList_Type">
    <xs:sequence>
      <xs:element name="provider" type="Provider_Type" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
    <xs:anyAttribute namespace="http://schemas.microsoft.com/XML-Document-Transform" processContents="strict"/>
  </xs:complexType>

  <xs:complexType name="Provider_Type">
    <xs:attribute name="invariantName" type="NonEmptyString_Type" use="required" />
    <xs:attribute name="type" type="NonEmptyString_Type" use="required" />
    <xs:anyAttribute namespace="http://schemas.microsoft.com/XML-Document-Transform" processContents="strict"/>
  </xs:complexType>

  <xs:complexType name="ContextList_Type">
    <xs:sequence>
      <xs:element name="context" type="Context_Type" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
    <xs:anyAttribute namespace="http://schemas.microsoft.com/XML-Document-Transform" processContents="strict"/>
  </xs:complexType>

  <xs:complexType name="Context_Type">
    <xs:choice>
      <xs:element name="databaseInitializer" type="ElementWithTypeAndParameters_Type" minOccurs="0" maxOccurs="1" />
    </xs:choice>
    <xs:attribute name="type" type="NonEmptyString_Type" use="required" />
    <xs:attribute name="disableDatabaseInitialization" type="SmallBoolean_Type" use="optional" />
    <xs:attribute name="commandTimeout" type="xs:int" use="optional" />
    <xs:anyAttribute namespace="http://schemas.microsoft.com/XML-Document-Transform" processContents="strict"/>
  </xs:complexType>

  <xs:complexType name="InterceptorList_Type">
    <xs:sequence>
      <xs:element name="interceptor" type="ElementWithTypeAndParameters_Type" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
    <xs:anyAttribute namespace="http://schemas.microsoft.com/XML-Document-Transform" processContents="strict"/>
  </xs:complexType>

  <xs:complexType name="ElementWithTypeAndParameters_Type">
    <xs:choice>
      <xs:element name="parameters" type="ParameterList_Type" minOccurs="0" maxOccurs="1" />
    </xs:choice>
    <xs:attribute name="type" type="NonEmptyString_Type" use="required" />
    <xs:anyAttribute namespace="http://schemas.microsoft.com/XML-Document-Transform" processContents="strict"/>
  </xs:complexType>

  <xs:complexType name="ParameterList_Type">
    <xs:sequence>
      <xs:element name="parameter" type="Parameter_Type" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
    <xs:anyAttribute namespace="http://schemas.microsoft.com/XML-Document-Transform" processContents="strict"/>
  </xs:complexType>

  <xs:complexType name="Parameter_Type">
    <xs:attribute name="value" type="NonEmptyString_Type" use="required" />
    <xs:attribute name="type" type="NonEmptyString_Type" use="optional" />
    <xs:anyAttribute namespace="http://schemas.microsoft.com/XML-Document-Transform" processContents="strict"/>
  </xs:complexType>

  <xs:simpleType name="SmallBoolean_Type">
    <xs:restriction base="xs:NMTOKEN">
      <xs:enumeration value="false" />
      <xs:enumeration value="true" />
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="NonEmptyString_Type">
    <xs:restriction base="xs:string">
      <xs:minLength value="1" />
    </xs:restriction>
  </xs:simpleType>

</xs:schema>
These 2 files can be found at this link:

Source:
http://www.asp.net/visual-studio/overview/2013/release-notes
http://stackoverflow.com/questions/19102831/how-to-install-asp-net-mvc-5-in-visual-studio-2012
http://stackoverflow.com/questions/18288215/how-can-i-add-the-mvc-5-project-template-to-vs-2012
http://stackoverflow.com/questions/20607648/no-asp-net-mvc-5-web-application-template-on-vs-2012
http://stackoverflow.com/questions/20068833/the-element-entityframework-has-invalid-child-element-entity-framework

No comments:

Post a Comment