MVC3/4/5/6 layout page and razor syntax

Source: Internet
Author: User

I. Directory structure

Second, Razor grammar

Code block: @{}, such as: @{html.raw ("");}

@if () {}

@switch () {}

@for () {}

@foreach () {}

@while () {}

@do {}while () {}

The statement in the code block ends with a semicolon

Expression or variable: start at @, such as: @User. Name or @ (User.Name + "welcome You"), @ ()

Variables can be declared by Var and can only be in blocks of code, such as: @{var abc= "AAA";}

Method invocation: Starts at @, such as: @Html. Encode ("")

Single-line text output: @: Directly after output text

Multiline text output: Use any label <span> text content </span>

e-Mail can be automatically identified:[email protected]

If you mistakenly believe that the mailbox can be [email protected] (UserName)

Output @ symbol: @@ 会 output an @ character

Third, layout page

~/views/_viewstart.cshtml is the page that will be referenced when all view pages (without a partial attempt page) are started, with the default layout page address:

" ~/views/shared/_layout.cshtml ";

~/views/shared/_layout.cshtml is the default layout page for the entire project:

<! DOCTYPE html>"Utf-8"/> <meta name="Viewport"Content="Width=device-width"/> <title> @ViewBag .title</title>    @* Load css*@ @RenderPage ("~/views/shared/_css.cshtml")    @* Custom CSS: Can be selected by the content page to fill *@ @RenderSection ("Customcss")    @* Custom Header JS: Can be filled by the content page self-selection *@ @RenderSection ("Customjs_header",false)    @* Load header*@ @RenderPage ("~/views/shared/_header.cshtml")    @* Load Body content page fill content *@ @RenderBody () @* Load footer*@ @RenderPage ("~/views/shared/_footer.cshtml")    @* Load js*@ @RenderPage ("~/views/shared/_js.cshtml")    @* Custom Foot JS: Can be filled by the content page self-selection *@ @RenderSection ("Customjs_footer",false)    @* Determine if the content page has rendered this node, if rendered, does not show default *@ @if (issectiondefined ("replaceablecontent") {@RenderSection ("replaceablecontent")    }    Else    {            }</body>

Renderpage ("page address"): Renders the contents of a page, typically used to load headers and footers

Rendersection ("node name", whether the content page must be rendered): Defines a node that the content page chooses to render

Renderbody (): All content on the content page is rendered to this location, equivalent to a placeholder for a content page

Issectiondefined: Checks whether the content page has already rendered this node, is the node that renders the content page, and no displays the default

MVC sets the name of a file that begins with an underscore by using the Web to request browsing individually, as follows:

_layout.cshtml

_header.cshtml

_footer.cshtml

Root directory: "~" represents the program root directory, such as: View ("~/views/home/about.cshtml");

Four, view page

@{Viewbag.title="Index"; //Layout = null;//each page can specify a layout page individually} @using System.Data.Common @model system.string@section customcss{<style>Div {height:20px;        margin:10px 10px 10px 10px; }    </style>} @section customjs_header{<script type="Text/javascript">(function Testheaderfun () {Console.log ("Js_header");    })(); </script>} @section customjs_footer{<script type="Text/javascript">(function Testfooterfun () {Console.log ("Js_footer");    })(); </script>}<div style="border:1px Solid Purple">I'm the body @Model. Length @{Html.raw ("");} @{Html.renderaction ("actionpartial","Home");} @{html.renderpartial ("viewpartial");} @Html. Action ("actionpartial","Home") @Html. Partial ("viewpartial")</div>

The Layout property can define whether the current page has a reference layout page null is not referenced, or can be defined to refer to other layout pages: layout = "~/views/shared/_layout_nologin.cshtml";

Introducing namespaces with using using: @using System.Data.Common

Sets the data type of return View ("") returned by the action: @model

The page is capitalized with the @Model first letter: @Model

Implement custom nodes for layout pages: @section node name {} such as: @section customcss{}

Output HTML content: The default MVC is to encode HTML tags in order to prevent XSS attacks if you want to output tags using @html.raw ("")

Introduce a partial attempt:

1, need to request the background action: (Action can be added childactiononly tag to prevent independent access)

@Html. Action ("Actionpartial", "Home")

@{html.renderaction ("Actionpartial", "Home");}

2. Load View directly

@Html. Partial ("Viewpartial")

@{html.renderpartial ("viewpartial");}

The difference between the two is that the former return mvchtmlstring object in the parsing to the current page, directly call, the latter directly to the content output to TextWriter, need code block call, theoretically the latter less than the former conversion should be slightly better performance, but this is too small.

V. Other

MVC also has three ways to interact with data

ViewData: The dictionary type (Dictionary), which passes the data by action to the view, the life cycle only in the current view, with the viewdata["Key" mode to access the value, for manual conversion type;

ViewBag: Dynamic type, which passes data by action to view, life cycle only in current view, Viewbag.key to access values, for no conversion type, underlying nature or viewdata only more layer dynamic control;

TempData: Dictionary type (Dictionary), you can pass data in any action and view, the life cycle is automatically deleted after the call, to tmpdata["Key"] mode to access the value, for manual conversion type;

Razor IntelliSense:

In the cshtml page after the smart prompt, if we define a class, and in view want to intelligently prompt this class of information, in the View/web.config file, open will find the following node:

<system.web.webPages.razor> "System.Web.Mvc.MvcWebRazorHostFactory, SYSTEM.WEB.MVC, version=4.0.0.0, Culture=neutral, publickeytoken= 31bf3856ad364e35"/> <pages pagebasetype="System.Web.Mvc.WebViewPage"> <namespaces> <addnamespace="SYSTEM.WEB.MVC"/> <addnamespace="System.Web.Mvc.Ajax"/> <addnamespace="System.Web.Mvc.Html"/> <addnamespace="System.Web.Optimization"/> <addnamespace="System.Web.Routing"/> </namespaces> </pages> </system.web.webPages.razor>

Simply add the namespace of the class you want to use, just like using it;

OutputCache output cache:

By using the OutputCache feature, we implemented a cache of objects. Therefore, when duplicate requests occur, it is possible to automatically output cache information to avoid querying the database frequently, reduce the burden on the server and improve the response speed.

[HttpGet]//set allow only get or post requests PublicActionResult Index (intID) {viewdata["ID"] =ID; Viewbag.id=ID; tempdata["ID"] =ID; returnView (String.Empty);} [OutputCache (Duration= -)]//setting the page output cache[Childactiononly]//defining this action can only be called by a child action PublicActionResult actionpartial () {returnPartialview ();}

MVC3/4/5/6 layout page and razor syntax

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.