DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Using Razor V2 in ASP.NET MVC 3

Imran Baloch user avatar by
Imran Baloch
·
Apr. 28, 12 · · Interview
Like (0)
Save
Tweet
12.82K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction:

Razor view engine's popularity and usage is continuously increasing because it is very easy to learn and write. Also, it is very clean and easy to maintain. The beauty of Razor is that you can host it in any application not just in ASP.NET MVC or ASP.NET Web Pages. The latest version of Razor is 2 which is released with ASP.NET MVC 4 and ASP.NET Web Pages 2. Razor V2 includes a bunch of new features. With the cool new features of Razor, you may want to use these features in your ASP.NET MVC 3 application. In this article, I will show you how you can use Razor V2 in ASP.NET MVC 3 application.

Description:

For testing purpose, you can create a new ASP.NET MVC 3(Razor) application. From Solution Explorer, remove the reference of System.Web.WebPages 1.0 and System.Web.Helpers 1.0. Then, add the reference of System.Web.WebPages 2.0 and System.Web.Helpers 2.0.

  

Inside root web.config file, add/update these settings,  

<appSettings>
            <add key="webpages:Version" value="2.0.0.0"/>
            <add key="webpages:Enabled" value="true" />
            <add key="PreserveLoginUrl" value="true" />
.............................................................................................................................
.............................................................................................................................
.............................................................................................................................
      <add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
.............................................................................................................................
.............................................................................................................................
.............................................................................................................................
      <dependentAssembly>
            <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
             <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
	    <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
.............................................................................................................................
.............................................................................................................................
.............................................................................................................................

Inside Views web.config file, add/update these settings,

<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" />

Now you can easily use Razor V2 features in inside ASP.NET MVC 3 view.

      @{
            int? nullValue = null;
      }

      <a href="~/WebForm1.aspx" class="@nullValue">MyLink</a>

Note: Before doing this, make sure you have installed ASP.NET MVC 4. Otherwise the trick will fail.

Summary:

There is a bunch of improvement and new features in Razor V2. In this article, I showed you how you can add Razor V2 inside ASP.NET MVC 3 application. I have also attached a sample ASP.NET MVC 3 application with above changes. Hopefully you will enjoy this article too.
ASP.NET MVC ASP.NET

Published at DZone with permission of Imran Baloch, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is HttpSession in Servlets?
  • Adaptive Change Management: A DevOps Approach to Change Management
  • Java Outsourcing, a Strong Business, and Management Approaches
  • How to Utilize Python Machine Learning Models

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo