Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
使用@符號后接C#或VB.NET語句的方式。
1)變量
@后直接變量即可
2)代碼塊
為使用表達式或多行代碼,@后跟大括號將多行代碼包括在大括號中
3)“+”
對于加號連接的兩個字符串變量或屬性,使用小括號將他們括起來
4)插入HTML或文字
每一行前面加上“@:”
5)使用注釋
使用@*和*@將要注釋的部分包起來
6)用@@在頁面上顯示@
在一個View中引入此頁所需程序集的命名空間。
還可以在web.config中配置命名空間,不過將對所有的View起作用。
<system.web.webPages.razor> <pages pageBaseType="System.Web.Mvc.WebViewPage"> <namespaces > <add namespace="System.Web.Mvc"/> <add namespace="WebApplication.Models"/> </namespaces> </pages> </system.web.webPages.razor>
指定頁面所用模型的類型。
使用自定義函數。這種方法有可能將一部分數據處理邏輯放到了頁面中,所以盡量不用。
例子:
定義函數
@helper CheckHelp(int i1,int i2) { if (i1 > i2) { @i1 } else { @i2 } }
使用函數
<h3>@CheckHelp(10,1111)</h3>
定義一個方法供當前頁使用,若使用IHtmlString作為方法的返回值,則可將其回傳給當前頁。
例子:
定義函數
@functions { public int CheckFunc(int i1, int i2) { if (i1 > i2) { return i1; } else { return i2; } } }
使用函數
<h3>@CheckFunc(10, 12111)</h3>
使用方式為@后跟輔助方法,注意沒有“;”,否則分號也會顯示在頁面上。
有幾個重載方法,選參數最多的一個
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes);
linkText:超連接名稱
actionName:操作名稱
controllerName:控制器名稱
protocol:URL 協議,如“http”或“https”。
hostName:URL 的主機名
fragment:URL 片段名稱(定位點名稱)
routeValues:路由參數
htmlAttributes:HTML 特性
例:
@Html.ActionLink("一個連接", "About")
對應的html代碼
<a href="/MVCPointApp/Home/About">一個連接</a>
有幾個重載方法,選參數最多的一個
public static MvcHtmlString RouteLink(this HtmlHelper htmlHelper, string linkText, string routeName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes);
routeName:路由名稱
其他參數同ActionLink
還可以使用強類型的輔助方法,一般是以For結尾。
控制器
public ActionResult TestViewData(ModelF mf) { ViewData.Model=new ModelF { Field=mf.Field, Field2=mf.Field2 }; return View("Index"); }
模型
public class ModelF { public string Field { get; set; } [Range(typeof(DateTime), "1/1/2018", "1/1/2019")] public DateTime Field2 { get; set; } }
視圖Index.cshtml
@using (Html.BeginForm("TestViewData", "Home")) { @Html.ValidationSummary() <input id="filed" name="Field2" type="text" placeholder="請輸入" value="" /> <input type="submit" value="提交" /> }
測試,輸入1/1/2020,執行結果為:
為了能顯示字段的中文名稱使用DisplayName
public class ModelF { public string Field { get; set; } [Range(typeof(DateTime), "1/1/2018", "1/1/2019")] [DisplayName("[這個字段]")] public DateTime Field2 { get; set; } }
執行結果為
由于class是C#保留關鍵字,因此設置class特性時要使用@
@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { @class="loginForm" })) { }
HTML輔助方法會將下劃線渲染為連字符,因此要表達含有連字符的特性,那么使用下劃線
Html.BeginForm("Login", "Account", FormMethod.Post, new { vla_input=true})
返回URI字符串
· 視圖放在Views文件夾下
· Views文件夾的子文件夾名稱為控制器名稱
· 視圖名稱可以是控制器操作方法名稱也可以不是,若不是控制器操作方法名稱,控制器返回視圖時要指定視圖名。
· Views文件夾下的Shared保存多個控制器共享的視圖
視圖定位規則是,先在Views文件夾中找對應控制器及控制器方法的視圖,沒有找到就到Shared文件夾下找。
· Views文件夾下_ViewStart.cshtml文件指定默認的模板,這個視圖先于任何試圖運行。
· 使用WebPageBase.Layout加載布局模板
· 使用@Html.Partial幫助方法加載部分視圖
· 使用@section定義指定內容的節,然后使用WebPageBase.RenderSection加載指定的節,使用public HelperResult RenderSection(string name, bool required);required=true,那么節必須已經定義,否則拋異常。
· @Styles.Render和@Scripts.Render捆綁和壓縮css、js
App_Start文件夾下BundleConfig類中
public static void RegisterBundles(BundleCollection bundles) { //多個文件用逗號分隔 bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include( "~/Scripts/jquery.cookie.js", "~/Scripts/jquery.hoverDelay.js", "~/Scripts/jquery.pagination.js", "~/Scripts/jquery.form.js", "~/Scripts/json2.js", "~/Scripts/hydss.js", "~/Scripts/hydss.utility.js")); bundles.Add(new StyleBundle("~/Content/css/base").Include( "~/Content/css/common.css", "~/Content/css/dev.css")); }
頁面中使用已經捆綁并壓縮的css和js,使用規則是:css文件置頂、js文件置地
@Styles.Render("~/Content/css/base") @Scripts.Render("~/bundles/jqueryval")
使用WebPageBase.Layout加載模板覆蓋_ViewStart.cshtml文件指定默認的模板
例如:
_ViewStart.cshtml文件如下
@{ Layout="~/Views/Shared/_Layout.cshtml"; }
Index.cshtml文件如下
@{ ViewBag.Title="Home Page"; Layout="~/Views/Shared/_LayoutOther.cshtml"; } <div class="jumbotron"> @*具體內容*@ </div>
如果沒有Layout="~/Views/Shared/_LayoutOther.cshtml";這行代碼,那么此視圖文件將使用_ViewStart.cshtml中的_Layout.cshtml這個模板,但這里Index.cshtml文件使用的是另一個模板_LayoutOther.cshtml
實際項目中可能會有不止一種布局,針對多種布局,既能滿足這種需求要能盡可能地代碼復用。
創建父模板_Layout.cshtml
<!DOCTYPE html> <html> <head> <title>@ViewBag.Title</title> <link rel="icon" href="~/favicon.ico" /> <link rel="shortcut Icon" href="~/favicon.ico" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="imagetoolbar" content="no" /> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="keywords" content="詞1 詞2" /> <meta name="description" content="網站的主題" /> @Styles.Render("~/Content/css/base") @Scripts.Render("~/bundles/jquery") @*加載HeaderSection節*@ @RenderSection("HeaderSection", false) </head> <body> @*加載主體*@ @RenderBody() <img id="loading" style="display:none;position:fixed;top:50%;left:50%;" src="@Url.Content("~/Content/images/loading.gif")" title="加載中..." alt="加載中..." /> <a href="javascript:void(0);" id="backToTop" title="回到頂部"></a> @*加載腳本*@ @Scripts.Render("~/bundles/jqueryval") @*加載FooterSection節*@ @RenderSection("FooterSection", false) </body> </html>
創建子模板_LayoutOther.cshtml
@{ Layout="~/Views/Shared/_Layout.cshtml"; } @*定義HeaderSection節*@ @section HeaderSection{ @RenderSection("HeaderSection", false) } @*加載頁頭*@ @Html.Partial("_header") @*加載主體*@ @RenderBody() @*加載頁腳*@ @Html.Partial("_footer") @*定義FooterSection節*@ @section FooterSection{ @RenderSection("FooterSection", false) }
分析
_LayoutOther.cshtml視圖使用了_Layout.cshtml視圖文件,_Layout.cshtml中@RenderSection來加載FooterSection和HeaderSection節,而這個節定義在_LayoutOther.cshtml中,不過沒有具體內容;_LayoutOther.cshtml中定義的FooterSection和HeaderSection又各自加載其他也面定義的FooterSection和HeaderSection節,所以可以在使用_LayoutOther.cshtml中靈活定義FooterSection和HeaderSection節,可以想象這樣一個場景,每個頁面都需要加載js文件,而他們既有共用的js文件,又有非共用的js文件,那么可以在使用_LayoutOther.cshtml的視圖中定義section 節來加載只有此頁面使用的js文件,而把公共的js文件放在_Layout.cshtml視圖文件中
例如Index.cshtml定義@section FooterSection{
@Scripts.Render("~/bundles/index")
}
這個節加載只供Index.cshtml這個頁面實用的js,這樣其他不需要這個js的頁面就不必加載這個js,從而達到減少頁面加載文件的目的進而優化了頁面。
配合@Html.Action方法使用控制器操作返回分部視圖
視圖中使用@Html.Action("TestPy"),控制器如下
public ActionResult TestFrom() { return PartialView("TestPy"); }
或者在視圖中使用@{Html.RenderAction("TestPy");},注意這種內聯視圖和Html.Action使用的區別。
使用Html.Partial直接調用部分視圖而不是通過控制器操作方法。
@Html.Partial("_header")
視圖代碼
@using (Html.BeginForm("TestFrom", "Home")) { <input id="UserName" name="UserName" type="text" placeholder="請輸入用戶名" value="" /> <input id="Password" name="Password" type="password" placeholder="請輸入密碼" value="" /> <input type="submit" value="提交"/> }
控制器代碼
public ActionResult TestFrom(FormCollection c) { var un=c["UserName"]; var pw=c["Password"]; ViewBag.Un=un; ViewBag.Pw=pw; return PartialView("TestPy"); }
返回值類型為IHtmlString,IHtmlString 是一個接口
public static IHtmlString HYSubString(this HtmlHelper helper, string param) { //字符串 String ret=...... return helper.Raw(ret); }
1. Jess Chadwick/Todd Snyder/Hrusikesh Panda,徐雷/徐揚
譯。ASP.NET MVC4 Web編程
2. Jon Galloway/Phil Haack/Brad Wilson/K. Scott Allen,孫遠帥/鄒權譯 ASP.NET MVC4 高級編程(第四版)
3. ASP.NET MVC4開發指南,黃保翕
4. ASP.NET MVC4框架揭秘,蔣金楠
5. https://www.asp.net/mvc
置頂王》是圖加軟件提供的免費桌面工具!通過它可輕松將各種 Windows 窗體(包括無標題欄窗體)置頂或取消置頂。
【一鍵置頂當前窗體】(視頻教程)
在當前窗體的標題欄單擊右鍵,在鼠標指針左邊會出現一個“皇冠”圖標;
將鼠標指針移入“皇冠”圖標內,即可置頂當前窗體。
【一鍵取消置頂當前窗體】
在當前窗體的標題欄單擊右鍵,在鼠標指針左邊會出現一個“皇冠”圖標;
將鼠標指針移入“皇冠”圖標內,即可取消置頂當前窗體。
來源:http://www.3h3.com/soft/257678.html
天查Html手冊時,又有了新的發現。也就這機會,好好總結下HTML中Meta的使用。
HTML <meta> 標簽,所有瀏覽器都支持 <meta> 標簽。它提供關于HTML文檔的元數據。元數據不會顯示在頁面上,但是對于機器是可讀的。它可用于瀏覽器(如何顯示內容或重新加載頁面),對搜索引擎和更新頻度的描述和關鍵詞,或其他 web 服務。
<meta> 標簽位于文檔的頭部,不包含任何內容。<meta> 標簽的屬性定義了與文檔相關聯的名稱/值對。
在 HTML 中,<meta> 標簽沒有結束標簽,在 XHTML 中,<meta> 標簽必須被正確地關閉。
必要屬性
屬性值描述contentsome text定義與http-equiv或name屬性相關的元信息
可選屬性
屬性值描述http-equivcontent-type / expire / refresh / set-cookie把content屬性關聯到HTTP頭部。nameauthor / description / keywords / generator / revised / others把 content 屬性關聯到一個名稱。contentsome text定義用于翻譯 content 屬性值的格式。
關鍵詞:類似這樣的 meta 標簽可能對于進入搜索引擎的索引有幫助.使用人們可能會搜索,并準確描述網頁上所提供信息的描述性和代表性關鍵字及短語。標記內容太短,則搜索引擎可能不會認為這些內容相關,標記不應超過 874 個字符。
<meta name="keywords" content="HTML,ASP,PHP,SQL">
頁面描述,每個網頁都應有一個不超過 150 個字符且能準確反映網頁內容的描述標簽
<meta name="description" content="your description">
搜索引擎索引方式,robotterms是一組使用逗號(,)分割的值,通常有如下幾種取值:none,noindex,nofollow,all,index和follow。確保正確使用nofollow和noindex屬性值。
<meta name="robots" content="index,follow" />
<!--
all:文件將被檢索,且頁面上的鏈接可以被查詢;
none:文件將不被檢索,且頁面上的鏈接不可以被查詢;
index:文件將被檢索;
follow:頁面上的鏈接可以被查詢;
noindex:文件將不被檢索;
nofollow:頁面上的鏈接不可以被查詢。
-->
頁面重定向和刷新:content內的數字代表時間(秒),既多少時間后刷新。如果加url,則會重定向到指定網頁(搜索引擎能夠自動檢測,也很容易被引擎視作誤導而受到懲罰)。
<meta http-equiv="Refresh" content="5;url=http://www.w3school.com.cn" /><!--5秒鐘后跳轉到http://www.w3school.com.cn-->
<meta http-equiv="Refresh" content="5;" /><!--每5秒鐘刷新一下頁面-->
viewport:能優化移動瀏覽器的顯示。如果不是響應式網站,不要使用initial-scale或者禁用縮放。大部分4.7-5寸設備的viewport寬設為360px;5.5寸設備設為400px;iphone6設為375px;ipone6 plus設為414px。很多人使用initial-scale=1到非響應式網站上,這會讓網站以100%寬度渲染,用戶需要手動移動頁面或者縮放。如果和initial-scale=1同時使用user-scalable=no或maximum-scale=1,則用戶將不能放大/縮小網頁來看到全部的內容。
content 參數:
width viewport 寬度(數值/device-width)
height viewport 高度(數值/device-height)
initial-scale 初始縮放比例
maximum-scale 最大縮放比例
minimum-scale 最小縮放比例
user-scalable 是否允許用戶縮放(yes/no)
<meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0, user-scalable=no"/>
<!-- `width=device-width` 會導致 iPhone 5 添加到主屏后以 WebApp 全屏模式打開頁面時出現黑邊 -->
各瀏覽器平臺
Microsoft Internet Explorer
<!-- 優先使用最新的ie版本 -->
<meta http-equiv="x-ua-compatible" content="ie=edge">
<!-- 是否開啟cleartype顯示效果 -->
<meta http-equiv="cleartype" content="on">
<meta name="skype_toolbar" content="skype_toolbar_parser_compatible"><!-- 關于X-UA-Compatible -->
<meta http-equiv="X-UA-Compatible" content="IE=6" ><!-- 使用IE6 -->
<meta http-equiv="X-UA-Compatible" content="IE=7" ><!-- 使用IE7 -->
<meta http-equiv="X-UA-Compatible" content="IE=8" ><!-- 使用IE8 --><!-- Pinned Site -->
<!-- IE 10 / Windows 8 -->
<meta name="msapplication-TileImage" content="pinned-tile-144.png">
<meta name="msapplication-TileColor" content="#009900">
<!-- IE 11 / Windows 9.1 -->
<meta name="msapplication-config" content="ieconfig.xml">
Google Chrome
<!-- 優先使用最新的chrome版本 -->
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
<!-- 禁止自動翻譯 -->
<meta name="google" value="notranslate">
360瀏覽器
<!-- 選擇使用的瀏覽器解析內核 -->
<meta name="renderer" content="webkit|ie-comp|ie-stand">
UC手機瀏覽器
<!-- 將屏幕鎖定在特定的方向 -->
<meta name="screen-orientation" content="landscape/portrait">
<!-- 全屏顯示頁面 -->
<meta name="full-screen" content="yes">
<!-- 強制圖片顯示,即使是"text mode" -->
<meta name="imagemode" content="force">
<!-- 應用模式,默認將全屏,禁止長按菜單,禁止手勢,標準排版,強制圖片顯示。 -->
<meta name="browsermode" content="application">
<!-- 禁止夜間模式顯示 -->
<meta name="nightmode" content="disable">
<!-- 使用適屏模式顯示 -->
<meta name="layoutmode" content="fitscreen">
<!-- 當頁面有太多文字時禁止縮放 -->
<meta name="wap-font-scale" content="no">
QQ手機瀏覽器
<!-- 鎖定屏幕在特定方向 -->
<meta name="x5-orientation" content="landscape/portrait">
<!-- 全屏顯示 -->
<meta name="x5-fullscreen" content="true">
<!-- 頁面將以應用模式顯示 -->
<meta name="x5-page-mode" content="app">
Apple iOS
<!-- Smart App Banner -->
<meta name="apple-itunes-app" content="app-id=APP_ID,affiliate-data=AFFILIATE_ID,app-argument=SOME_TEXT">
<!-- 禁止自動探測并格式化手機號碼 -->
<meta name="format-detection" content="telephone=no">
<!-- Add to Home Screen添加到主屏 -->
<!-- 是否啟用 WebApp 全屏模式 -->
<meta name="apple-mobile-web-app-capable" content="yes">
<!-- 設置狀態欄的背景顏色,只有在 “apple-mobile-web-app-capable” content=”yes” 時生效 -->
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<!-- 添加到主屏后的標題 -->
<meta name="apple-mobile-web-app-title" content="App Title">
Google Android
<meta name="theme-color" content="#E64545">
<!-- 添加到主屏 -->
<meta name="mobile-web-app-capable" content="yes">
<!-- More info: https://developer.chrome.com/multidevice/android/installtohomescreen -->
App Links<!-- iOS -->
<meta property="al:ios:url" content="applinks://docs">
<meta property="al:ios:app_store_id" content="12345">
<meta property="al:ios:app_name" content="App Links">
<!-- Android -->
<meta property="al:android:url" content="applinks://docs">
<meta property="al:android:app_name" content="App Links">
<meta property="al:android:package" content="org.applinks">
<!-- Web Fallback -->
<meta property="al:web:url" content="http://applinks.org/documentation">
<!-- More info: http://applinks.org/documentation/ -->
其它常用的meta
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="format-detection"content="telephone=no, email=no" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" /><!-- 刪除蘋果默認的工具欄和菜單欄 -->
<meta name="apple-mobile-web-app-status-bar-style" content="black" /><!-- 設置蘋果工具欄顏色 -->
<meta name="format-detection" content="telphone=no, email=no" /><!-- 忽略頁面中的數字識別為電話,忽略email識別 -->
<!-- 啟用360瀏覽器的極速模式(webkit) -->
<meta name="renderer" content="webkit">
<!-- 避免IE使用兼容模式 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- 針對手持設備優化,主要是針對一些老的不識別viewport的瀏覽器,比如黑莓 -->
<meta name="HandheldFriendly" content="true">
<!-- 微軟的老式瀏覽器 -->
<meta name="MobileOptimized" content="320">
<!-- uc強制豎屏 -->
<meta name="screen-orientation" content="portrait">
<!-- QQ強制豎屏 -->
<meta name="x5-orientation" content="portrait">
<!-- UC強制全屏 -->
<meta name="full-screen" content="yes">
<!-- QQ強制全屏 -->
<meta name="x5-fullscreen" content="true">
<!-- UC應用模式 -->
<meta name="browsermode" content="application">
<!-- QQ應用模式 -->
<meta name="x5-page-mode" content="app">
<!-- windows phone 點擊無高光 -->
<meta name="msapplication-tap-highlight" content="no">
<!-- 適應移動端end -->
網頁編碼:以下兩種charset定義方式均可
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
禁止緩存:禁止瀏覽器從本地機的緩存中調閱頁面內容,網頁不保存在緩存中,每次訪問都刷新頁面。這樣設定,訪問者將無法脫機瀏覽
<meta http-equiv="Pragma" content="no-cache">
網頁過期:指定網頁在緩存中的過期時間,一旦網頁過期,必須到服務器上重新調閱。注意:必須使用GMT的時間格式,或直接設為0(數字表示多少時間后過期)
<Meta http-equiv="Expires" Content="Wed, 26 Feb 1997 08:21:57 GMT">
Cookie設置:注意:必須使用GMT的時間格式
<Meta http-equiv="Set-Cookie" Content="cookievalue=xxx; expires=Wednesday,21-Oct-98 16:14:21 GMT; path=/">
顯示窗口的設定:強制頁面在當前窗口以獨立頁面顯示,這個屬性是用來防止別人在框架里調用你的頁面。Content選項:_blank、_top、_self、_parent.
<Meta http-equiv="Widow-target" Content="_top">
進入與退出:這個是頁面被載入和調出時的一些特效。這個有好多特效,可以查詢Page-Exit去了解更多。
<Meta http-equiv="Page-Exit" Content="blendTrans(Duration=0.5)">
內容安全策略CSP(Content-Security-Policy),可以參考https://blog.csdn.net/u014465934/article/details/84199171
<meta http-equiv="Content-Security-Policy" content="script-src 'self'">
喜歡小編的可以點個贊關注小編哦,小編每天都會給大家分享文章。
我自己是一名從事了多年的前端老程序員,小編為大家準備了新出的前端編程學習資料,免費分享給大家!
如果你也想學習前端,可以觀看【置頂】文章。也可以私信【1】拿
*請認真填寫需求信息,我們會在24小時內與您取得聯系。