Web Pages Razor Learning

Source: Internet
Author: User

1. Web Pages Razor

Web Pages is one of three ASP. NET programming models that you use to create your ASP.

The other two programming models are Web Forms and MVC (model, View, Controller).

Web pages is the simplest programming model for ASP. It provides an easy way to combine HTML, CSS, JavaScript, and server code:

    • Easy to learn, read and use
    • Build around a single Web page
    • Similar to PHP and ASP
    • Server scripts use Visual Basic or C #
    • Full control over HTML, CSS, and JavaScript

Web Pages are extended through a programmable web Helpers, including databases, videos, images, social networks, and more.

2. Learning Practice

1. Development environment VS2010, SQL Server 2012

2. Project file layout:

Explanation of several names:

Before Web Startup: _appstart

Most server-side code is written in a separate Web page. For example, if a Web page contains an input form, the page typically contains the server code that is used to read the data.

However, by creating a page named _appstart in the site root, you can execute the startup code before the site is started. If the page exists,

ASP. NET will run this page first before any pages within the site are requested.

Typically _appstart is used for initialization of startup code and global values, such as counters and global names.

Note 1:_appstart should have the same file name extension as the Web page, such as _appstart.cshtml.

Note The 2:_appstart has an underscore prefix. Because of this, users cannot view the file directly.

Before each page: _pagestart

Just like running _appstart before the site starts, you can run code before any page in each folder.

For each folder in the Web, you can add a file named _pagestart.

Typically _pagestart is used to set the layout file for all pages in a folder, or to check the user's login before running the page.

Process

When the request arrives, ASP. NET checks to see if the _appstart exists. If it exists, and this is the first request to the site, run _appstart.

Then ASP. NET checks if the _pagestart exists. If present, run _pagestart before the requested page.

If you reference a call to Runpage () within _pagestart, you can specify where to run the requested page. Otherwise, _pagestart is run before the requested page.

The Code section explains:

Login.cshtml

@{
Layoutpage = "layout.cshtml";
This.  Layout = "layout.cshtml"; Using template layouts
@:d Along
}
@{

var database = Database.open ("database");
var data = database.   Query ("Select Top Psnname, corpname from MyPerson2"); Querying the database
var Grid = new WebGrid (data); Data binding Display
foreach (var item in data)
{
<li>the age is: @item. Psnname
</li>
<li>the name is: @item. Corpname
</li>
}
}
@section dalong33 {node layout test
<div id= "Grid" >
@grid. Gethtml ()
</div>
}
@{

var username = string. Empty;

Username = request["username"]; Fetching of request parameters
if (username! = null)
{
<form action= "Login" method= "POST" >
<label for= "username" >
User name </label><input type=text name= "username" [email protected]/><br>
<label for= "Pass" >
Password </label><input type= "text" name= "pass"/><br>
</form>
}
}

Layout.cshtml

<! DOCTYPE html>



<body>

@RenderBody ()

@RenderSection ("Dalong33", false); Node layout (false node not required)
</body>


The _appstart.cshtml app starts executing

@{

Context.Response.Write ("<script>alert (' dalong start app ') </script>");

}

_pagestart.cshtml each page executes the

@{

Output.write ();

Context.Response.Write ("<script>alert (' app ') </script>");
}

The interface of the test is relatively simple, no stickers.

Note For references to the required DLLs

    • Microsoft.Web.Infrastructure.dll
    • NuGet.Core.dll
    • System.Web.Helpers.dll
    • System.Web.Razor.dll
    • System.Web.WebPages.Administration.dll
    • System.Web.WebPages.Deployment.dll
    • System.Web.WebPages.dll
    • System.Web.WebPages.Razor.dll
    • WebMatrix.Data.dll
    • Webmatrix.webdata

Web Pages Razor Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.