ASP. Razor View Engine Programming reference

Source: Internet
Author: User
Tags tagname actionlink

Reprint Please specify source: http://surfsky.cnblogs.com

Rasor View Engine
Http://msdn.microsoft.com/zh-cn/library/ff849693.aspx
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=b7937c34-3b53-47b7-ae17-5a72fa700472&displaylang=en
Http://aspnet.codeplex.com/wikipage?title=WebPages&referringTitle=Home

Advantages: "Smart and simple"

It can be used as an MVC view engine, or it can be used to write traditional PHP-like web sites, very lightweight.


Grammar recognition
Embedding variables
<a href= "/products/details/@productId" >the product</a>
Expression (parentheses)
<p>your message: @ ("number is:" + number) </p>
code block (curly brackets)
@{
int number = 1;
String message = "number is" + number;
}
@{var myquote = @ "The person said:" "Hello, Today is Monday." ";}
<text> tags
if (i > 0) {
<text>;</text>
}
Smart Difference @
<p>mail[email protected]Telling: @Date .now.</p>
You can explicitly hit @@ 来 escape with another "@" character

Judge
@if (Products. Count = = 0) {
<p>sorry-no products in this category </p>
}else{
<p>we has a products for you!</p>
}
@if (DateTime.Now.Year = = 2010) {
<span>
The date: @DateTime. Now
</span>
}

Cycle
<ul id= "Products" >
@foreach (var p in products) {
<li>@p.name ([email protected]) </li>
}
</ul>

Template
<! DOCTYPE html>
<body>
<div> @RenderSection ("menu", Optional:true) </div>
<div> @RenderBody () </div>
<div> @RenderSection ("Footer", optional:true) </div>
</body>
-----------------------------
@{layoutpage = "sitelayout.cshtml";}
<p>current datetime: @DateTime .now</p>
@section menu{
<ul id= "Sub-menu" >
<li>item1</li>
<li>item2</li>
</ul>
}
@section footer{
<p>this is my custom footer</p>
}

Auxiliary functions
@Html. labelfor (M = m.productid)
@Html. textboxfor (M = m.productid)
@Html. validationmessagefor (M = m.productid)

Creating auxiliary functions
@helper productlisting (list<product> products) {
<ul>
@foreach (var p in products) {
<li>@p.name ([email protected]) </li>
}
</ul>
}
<div> @ProductListing (model.products) </div>

function parameters
@Grid. Render (
Data:Model.products,
TableStyle: "Grid",
HeaderStyle: "Head",
Alternationrowstyle: "Alt",
Columns:Grid.Columns (
Grid.column ("Name", "Product", Style: "Product"),
Grid.column ("Description", format:@ (<i> @item. description</i>),
Grid.column ("Price", Format:@<span>[email protected]</span>)
)
)

Function
@using System.Text;
@functions {
public static ihtmlstring Repeat (int times, func<int, object> template) {
StringBuilder builder = new StringBuilder ();
for (int i = 0;  I < times; i++) {
Builder. Append (Template (i));
}
return new htmlstring (builder. ToString ());
}
}
@Repeat (@<li>list item</li>);
@Repeat (@<li>list Item # @item </li>);

Type conversions
var mystringnum = "539";
var mystringbool = "True";
var mystringfloat = "41.432895";
var mystringdec = "10317.425";
var mydatestring = "12/27/2010";
-------------------------------
if (Mystringnum.isint () ==true)
var myintnumber = Mystringnum.asint ();
var MyVar = Mystringbool.asbool ();
var myfloatnum = Mystringfloat.asfloat ();
var mydecnum = Mystringdec.asdecimal ();
var newdate = Mydatestring.asdatetime ();


File
No extension is required to access the cshtml file. such as: ~/gallery/index
cshtml files starting with underscores cannot be run separately (typically as template files, common controls)
Several special files
_init.cshtml
@{
Set the layout page for the whole site
Layoutpage = "_master.cshtml";
}
_start.cshtml
@{
Websecurity.initializedatabaseconnection ("Photogallery", "Userprofiles", "UserId", "Email", true);
}


--------------------------------
--More
--------------------------------
@Inherits System.Web.Mvc.WebViewPage
View.title = "Home page";
Layoutpage = "~/views/shared/_layout.cshtml";
View.message

Login
Check
if (websecurity.isauthenticated) {
You are welcome to,<b> @WebSecurity. currentusername</b>!
@Html. ActionLink ("Logout", "LogOff", "account")
}
else{
@Html. ActionLink ("Login", "logon", "account")
}
@if (websecurity.isauthenticated) {
<span>welcome <b><a href= "@Href (" ~/account/changepassword ")" > @WebSecurity. currentusername</ a></b>!
[<a href= "@Href (" ~/account/logout ")" >Logout</a>]</span>
} else {
<span>[<a href= "@Href (" ~/account/login ")" >Login</a> | <a href= "@Href (" ~/account/register ")" >Register</a>]</span>
}
Login
Attempt to login to the Security object using provided Creds
if (Websecurity.login (username, password, rememberme)) {
Response.Redirect ("~/");
}
Logout
Websecurity.logout ();
Regist
Websecurity.createaccount (email, password, requireemailconfirmation)
Websecurity.confirmaccount (Confirmationtoken)
Websecurity.getuserid (email)
Websecurity.generatepasswordresettoken (email)
Password
Websecurity.resetpassword (Passwordresettoken, NewPassword)
Websecurity.changepassword (Websecurity.currentusername, CurrentPassword, NewPassword)

Template
@RenderPage ("/shared/_header.cshtml")
@RenderPage ("/shared/_footer.cshtml")
@RenderBody ()
@RenderSection ("Fffff")

Microsoft SQL Server Compact Edition
    var db = Database.openfile ("Database.sdf");
    var sql = "SELECT * FROM table1";
    var data = db. Query (SQL);
    database.execute (SQL)
    
FileUpload
    @ Fileupload.gethtml (
      initialnumberoffiles:1,
      Allowmorefilestobeadded:false,
      includeformtag:true,
       uploadtext: "Upload"
     )

Image
    webimage.resize ();
    webimage.flipvertical ();
           . FlipHorizontal ();
           . Flipleft ();
           . Flipright ();
    Webimage.addtextwatermark ();
    Webimage.addimagewatermark ();

Video
@Video. Flash (
Path: "Testflash.swf",
Width: "400",
Height: "600",
Play:true,
Loop:true;
Menu:false,
BgColor: "Red",
Quality: "Medium",
Scale: "Exactfit",
Windowmode: "Transparent"
);
@Video. MediaPlayer ()
@Video. Silverlight ()

Toolkit (Microsoft.WebPages.Helpers.Toolkit.dll)
Twitter
@Twitter. Profile ("haacked")
Facebook
Gravator
Recaptcha

Form Postback
<form action= "" method= "POST" >
<p>
<label for= "Text1" >first number:</label>
<input type= "text" name= "Text1"/>
</p>
<p>
<label for= "Text2" >second number:</label>
<input type= "text" name= "Text2"/>
</p>
<p><input type= "Submit" value= "ADD"/></p>
</form>
<p> @totalMessage </p>
@{
var totalmessage = "";
if (IsPost) {
var num1 = request["Text1"];
var num2 = request["Text2"];
var total = Num1. Asint () + num2. Asint ();
Totalmessage = "Total =" + total;
}
}

Mail
    mail.send (
      to:email, 
       Subject: "Please reset your password",  
      Body: "Use this password Reset token to reset your password. The token is: "+ Resettoken + @". Visit <a href= "" "+ Reseturl + @" ">" + Reseturl + "</a> to reset your password."
   );
    Mail.SmtpServer.IsEmpty ()
        
------- -------------------------
--Available MVC Helper and helper classes
--------------------------------
@Inherits System.Web.Mvc.WebViewPage
@Inherits system.web.mvc.webviewpage<ilist<rasorsample.models.category>
@model List<product>

@PageData ["Title"] for in-page data sharing, such as masterpage and contentpage sharing
@Href ("~/site.css") Get URL
@WebSecurity encapsulates user security-related functions
@UrlData [0] should be equivalent to request["..."]
@Html. Pagelink ("View", (String) similartags[i]. TagName, (String) similartags[i]. TagName)
<a href= "@HrefAttribute (" View ", tag. TagName) ">

ASP. Razor View Engine Programming reference

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.