in

Chennai .Net User Group

A platform that enables you to Learn, Share & Grow (India's first .Net user group)

Shiju Varghese's Blog

July 2010 - Posts

  • ASPX and Razor View Engines in ASP.NET MVC 3 Preview 1

    ASP.NET MVC is a highly extensible framework that allows you to use different view engines with ASP.NET MVC. ASP.NET MVC Preview 1 comes with two bult-in view engines: ASPX and Razor. When you create a new ASP.NET MVC 3 project,  you can see two project templates as shown in the following screen shot.



    You can select ASP.NET MVC 3 Web Application (APSX) for a pre-defined template for using ASPX syntax in the view pages and can use ASP.NET MVC 3 Web Application (Razor) for writing view pages with Razor syntax. In this post, I am creating two ASP.NET MVC 3 projects with the two different project templates so that we can easily compare the syntax between ASPX and Razor that would be helpful to learn Razor syntax.

    The below shows the folder structure of both ASPX project and Razor project.

    Folder Structure of ASPX project template

     Folder Structure of RAZOR project template

     



    The view page files in the Razor syntax is using a file extesion .cshtml. This would be vbhtml for a VB.NET application.

    Layout/Master page

    The below code blocks shows MasterPage in ASPX and Layout in Razor


    Site.Mater (ASPX)

     

    <%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

        <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>

        <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />

    </head>

     

    <body>

        <div class="page">

     

            <div id="header">

                <div id="title">

                    <h1>My MVC Application</h1>

                </div>

     

                <div id="logindisplay">

                    <% Html.RenderPartial("LogOnUserControl"); %>

                </div>

     

                <div id="menucontainer">

     

                    <ul id="menu">             

                        <li><%: Html.ActionLink("Home", "Index", "Home")%></li>

                        <li><%: Html.ActionLink("About", "About", "Home")%></li>

                    </ul>

     

                </div>

            </div>

     

            <div id="main">

                <asp:ContentPlaceHolder ID="MainContent" runat="server" />

     

                <div id="footer">

                </div>

            </div>

        </div>

    </body>

    </html>


    Layout.cshtml (Razor)


    @inherits System.Web.Mvc.WebViewPage

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

        <title>@View.Title</title>

        <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

    </head>

     

    <body>

        <div class="page">

     

            <div id="header">

                <div id="title">

                    <h1>My MVC Application</h1>

                </div>

     

                <div id="logindisplay">

                    @Html.Partial("_LogOnPartial")

                </div>

     

                <div id="menucontainer">

     

                    <ul id="menu">

                        <li>@Html.ActionLink("Home", "Index", "Home")</li>

                        <li>@Html.ActionLink("About", "About", "Home")</li>

                    </ul>

     

                </div>

            </div>

     

            <div id="main">

                @RenderBody()

                <div id="footer">

                </div>

            </div>

        </div>

    </body>

    </html>

     


    The @ character is using to specify the start of a code block in Razor and it does not require you to explicitly close the code-block that can be save lot of key strokes. Razor is using “layout pages” for supporting the Master page concept in ASPX. You can use  @Html.Partial instead of ASPX syntax Html.RenderPartial. The “RenderBody()” method is used for rendering the body content of view pages that is based on the this Layout page. The body content will be inferred by the @RenderBody method of the Layout page.

    Index View Page

    Let's have a look at the Index view page in the default MVC project template. The below is the view template pages of both Index.aspx (ASPX) and Index.cshtml(Razor)

    Index.aspx (ASPX)

     

    <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

     

    <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">

        Home Page

    </asp:Content>

     

    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

        <h2><%: View.Message %></h2>

        <p>

            To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc"

            title="ASP.NET MVC Website">http://asp.net/mvc</a>.

        </p>

    </asp:Content>

     

    Index.cshtml (Razor)

    @inherits System.Web.Mvc.WebViewPage

     

    @{

        View.Title = "Home Page";

        LayoutPage = "~/Views/Shared/_Layout.cshtml";

    }

     

    <h2>@View.Message</h2>

    <p>

        To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc"

        title="ASP.NET MVC Website">http://asp.net/mvc</a>.

    </p>

     

    The @{ } block is using to specify a multi-line code block in Razor as shown in the below code.

     

    @{

        View.Title = "Home Page";

        LayoutPage = "~/Views/Shared/_Layout.cshtml";

    }

     

    Conclusion

    The new Razor syntax allows the ASP.NET MVC developer to write view templates with very clean and elegant code. You can also mix with both ASPX pages and Razor pages in a single project.   

  • ASP.NET MVC 3 Preview 1 Released

     The ASP.NET team has released the first preview version of ASP.NET MVC 3. You can download the ASP.NET MVC 3 Preview 1 from here. The following are the summary of new features in the ASP.NET MVC 3 Preview 1.

    •  New Razor View Engine 
    •  Visual Studio Add View Dialog support for multiple view engines (pre-enabled with ASPX and Razor)
    •  New Dynamic ViewModel Property
    •  Global Action Filters – Define cross-cutting concerns globally and dynamically
    •  Improved Dependency Injection via a common service locator interface – First-class support for DI all over the place
    •  Support for .NET 4 Data Annotations and Metadata attributes

       For more details, checkout ScottGu's post  Introducing ASP.NET MVC 3 (Preview 1)

     

     

  • ASP.NET Page Helpers in WebMatrix

     The new WebMatrix beta is providing set of HTML helpers that can be use for common web site tasks. The below ASP.NET page within the WebMatrix is using some HTML helpers for integrating social networking web site features.

     

    <!DOCTYPE html>

    <html>

        <head>

             <title>Helpers Demo</title>

        </head>

        <body>      

         <table>    

         <tr>

            <td>Twitter Profile helper</td>

            <td>Twitter search helper</td>

          </tr>

          <tr>

            <td>@Twitter.Profile("shijucv")</td>

            <td>@Twitter.Search("asp.net mvc")</td>

          </tr>

        </table> 

        <p>

        @Gravatar.GetHtml("shiju.varghese@gmail.com", 50)

        </p>

        <p>

         @LinkShare.GetHtml("LinkShare","http://weblogs.asp.net/shijuvarghese")

        </p>

       <p>

        @Facebook.LikeButton("http://weblogs.asp.net/shijuvarghese")

       </p>

        </body>

    </html>

     

     

     The @ character is part of the new Razor syntax that represents the server-side code. In the above code block, we have added five HTML helpers on the ASP.NET web page. The Twitter.Profile would provide a twitter feed for a given user name and the Twitter.Search would provide twitter search for a text. The Gravatar helper can be use to render a Gravatar image for a account name. The  LinkShare.GetHtml helper can be use to render link share buttons for the major social networking web sites. The Facebook.LikeButton would provide a facebook like button for a given page link.

    The below screen shot is shown the output of the above ASP.NET page.

     

     

  • WebMatrix Beta Released

    Microsoft has announced the first beta release of Microsoft WebMatrix which is a lightweight development tool for web developers. WebMatrix can be installed side-by-side with Visual Studio 2010 and Visual Web Developer 2010 Express. The most interesting thing about the WebMatrix beta is that you can now play with the new Razor view engine for ASP.NET MVC. The ASP.NET team  haven't released the MVC View Engine for Razor, but WebMatrix includes ASP.NET Pages that using the new Razor syntax. You can download the WebMatrix beta from here.


    WebMatrix Resources


  • Microsoft MVP for ASP.NET

    MVP

    I would like to inform you that I’ve been awarded as Microsoft Most Valuable Professional (Microsoft MVP) for ASP.NET. This is a very proud moment of my professional life. I believe that this award is great honor to the ASP.NET MVC community. I have been involving with ASP.NET MVC since its first preview version and I am trying to spread the good information about ASP.NET MVC in the .NET community for the past few years. I really thank Microsoft for the MVP award and I really passionate on their web platform products especially on the great ASP.NET MVC framework. I would like to thank all of the readers of my blog and the Kerala Microsoft Users Group (k-mug.org) community for the great support.

    Follow me on Twitter at @shijucv

Copyright © 2002-2008 Chennai .Net User Group. All Rights Reserved. Microsoft and Microsoft logo's are trademarks of Microsoft Corporation