Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use Razor View Engine in Asp.Net

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 554
    Comment on it

    "Introduction to Razor View Engine"

        Razor is used to create dynamic Web pages, it supports both C# as well as VB.Net. Razor is used like a markup syntax for adding server-side code to web pages inorder to make them dynamic. It is much like a scripting Language, the file extension of Razor is .cshtml.

    Some key points of Razor view engine :

    1. The code blocks in the Razor are enclosed inside @{.........}.
    2. All the variables and functions start with @.
    3. Each code line ends with a semicolon.
    4. Strings in the Razor are enclosed with the quotation marks.
    5. Comment inside Razor can be given as @.........@.
    6. Variables can be declared with the var keyword.

    Example:

    Step 1: Create a Asp.Net MVC application.

    Step 2: Now write a Controller (ControllerName=mayank) and add a View to it.

    Step 3: The default View looks like:

    @{
        ViewBag.Title = "mayank";
     }
    
    <h2>mayank</h2>
    

    Step 4: Now write the following code:

    @{
        ViewBag.Title = "mayank";
        var name = "Mayank Srivastava";
        var address = "Delhi";
        var id = 12;
    }
    
    <h2>mayank</h2>
    
    @*Inline-expression*@
    
    <p>
    
        The Name of the Employee is:@name
    </p>
    
    <p>
    
        The Address of the Employee is: @address
    </p>
    
    <p>
    
        The Id of the Employee is:@id
    </p>
    

    Step 5: The output will be:

    mayank
    
    The Name of the Employee is:Mayank Srivastava
    
    The Address of the Employee is: Delhi
    
    The Id of the Employee is:12
    
     2015 - My ASP.NET Application
    

    Happy Coding..!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: