zoukankan      html  css  js  c++  java
  • Hello, Razor!

    Razor is a template syntax that allows you to combine code and content in a fluid and expressive manner. Though it introduces a few symbols and keywords, Razor is not a new language. Instead, Razor lets you write code using languages you probably already know, such as C# or Visual Basic .Net.

    Razor’s learning curve is very short, as it lets you work with you existing skills rather than requiring you to learn an entirely new language. Therefore, if you know how to write HTML and make a .Net API call, you can easily write markup like the following:

    <div>This page rendered at @DateTime.Now</div>

    which produces output like this:

    <div>This page rendered at 12/7/1941 7:38:00 AM</div>

    This example begins with a standard HTML tag ( the <div> tag ), followed by a bit of static text. In the midst of all of this is some dynamic text rendered via a call to the .NET System.DateTime object, followed by the closing (</div>) tag.

    Razor’s intelligent parser allows developers to be more expressive with their logic and make easier transitions between code and makeup. The more advanced the makeup, the easier it is to see how Razor’s syntax is cleaner and more expressive than the Web Forms syntax. Compare the following scenarios, each one showing the Razor markup and Web Forms markup required to produce the same HTML:

    if/else statement using Web Forms syntax

    <% if(User.IsAuthentiated){ %>
        <span>Hello, <%= User.UserName %>!</span>
    <% } %>
    <% else { %>
        <span>Please <%= Html.ActionLink("log in") %></span>
    <% } %>

    if/else statement using Razor syntax

    @if(User.IsAuthenticated) {
        <span>Hello, @User.Username!</span>
    } else {
        <span>Please @Html.ActionLink("log in")</span>
    }
     
    foreach loop using Web Forms syntax
    <ul>
    <% foreach(var post in blogPosts){ %>
        <li>        
            <a href=”&lt;%= post.Href %&gt;”>            
                <%= post.Title %>       
            </a>   
        </li>
    <% } %>
    </ul>

    foreach loop using Razor syntax

    <ul>
        @foreach( var post in blogPosts){
            <li>
                <a href="@post.Href">@post.Title</a>
            </li>
    }
    </ul>

    Though the difference between the Web Forms syntax and Razor syntax is only a few characters, those characters make a big difference the readability of the markup! One of the loudest complains from developers attempting to use Web Forms to author dynamic markup is that its “angle-bracket” syntax is so verbose that it can distract from the page’s logic and content. Additionally, because the Web Forms syntax itself so closely resembles HTML markup, it is often diffcult to determine which parts of the template are code and which are markup.

    In direct contrast, Razor use minimal market to perform the same tasks, What’s more, Razor’s syntax was deliberately designed to blend in with HTML, not conflict with it.

  • 相关阅读:
    视频直播:Windows中各类画面源的截取和合成方法总结
    短视频技术详解:Android端的短视频开发技术
    视频直播关键技术:流畅、拥塞和延时追赶
    视频直播技术详解:直播的推流调度
    直播技术:从性能参数到业务大数据,浅谈直播CDN服务监控
    音视频通话:小议音频处理与压缩技术
    pip命令报错“no perl script found in input”
    python常见面试题讲解(三)明明的随机数
    如何使用photoshop修改图片的像素大小(分辨率)
    VMware Workstation如何修改弹出释放快捷键
  • 原文地址:https://www.cnblogs.com/zhanghaiyu/p/3312199.html
Copyright © 2011-2022 走看看