/// <summary>/// 拍賣師控制類/// </summary>[NonController]public class AuctionController{}
常用特性
特性數據源
FromHeaderAttribute
請求頭數據
FromRouteAttribute
路由數據
FromBodyAttribute
請求體
FromFormAttribute
表單數據
FromQueryAttribute
查詢字符串
FromServicesAttribute
服務注冊
public IActionResult Say([FromForm]string name,[FromQuery]int age,[FromHeader] string salt,[FromBody] string content){return View();}
特性參數
通過特性修飾參數來影響綁定邏輯
靈活擴展
IActionResult
動作結果接口
具體實現
JsonResult:返回JSON結構數據
RedirectResult:跳轉到新地址
FileResult:返回文件
ViewResult:返回視圖內容
ContentResult:文本內容
第三課 視圖與表單
數據傳遞
ViewData
ViewBag
tempData
Model
Session
Cache
ViewDataViewBag
鍵值對
動態類型
索引器
ViewData的封裝
支持任意類型
動態屬性
TempDataCacheSession
視圖級別
應用程序級別
會話級別
只允許消費一次
服務器端保存
服務器端保存
可多次賦值
可設置有效期
鍵值對形式
鍵值對形式
鍵值對形式
Cache
與.NET Framework時代不同,一種全新實現
IMemoryCache接口
依賴注入方式獲取
IMemoryCache.Get/Set操作數據
[Controller]public class Test : Controller{private readonly IMemoryCache _cache;public Test(IMemoryCache memoryCache){this._cache = memoryCache; }public IActionResult ReadCache(){_cache.Set("name","tom");_cache.Get("name");_cache.Set("age",30);_cache.Get("age");User tom = new User(){ Name = "admin",Pwd = "123456"};_cache.Set<User>("user",tom);_cache.Get<User>("user");return Content("ok");}}public class User{public string Name { get; set; }public string Pwd { get; set; }}
ViewStart
以_ViewStart.cshtml命名,固定名稱,不能更換
一般放在視圖所在目錄的根目錄下
自動執行,無需手工調用
不要再ViewStart中做大量的業務操作
ViewImport
以_ViewImport.cshtml命名,固定名稱,不能更換
只作引入操作
一般放在視圖所在目錄的根目錄下
自動執行,無需手工調用
視圖中可以使用@using關鍵字引入所需命名空間
通過ViewImport做全局性的命名空間引入,減少在每個頁面中引入的工作量
第四課 數據驗證
數據驗證特性ValidationAttribute
public abstract class ValidationAttribute : Attribute{/// <summary>Initializes a new instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationAttribute"></see> class.</summary>protected ValidationAttribute();/// <summary>Initializes a new instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationAttribute"></see> class by using the function that enables access to validation resources.</summary>/// <param name="errorMessageAccessor">The function that enables access to validation resources.</param>/// <exception cref="T:System.ArgumentNullException"><paramref name="errorMessageAccessor">errorMessageAccessor</paramref> is null.</exception>protected ValidationAttribute(Func<string> errorMessageAccessor);/// <summary>Initializes a new instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationAttribute"></see> class by using the error message to associate with a validation control.</summary>/// <param name="errorMessage">The error message to associate with a validation control.</param>protected ValidationAttribute(string errorMessage);/// <summary>Gets or sets an error message to associate with a validation control if validation fails.</summary>/// <returns>The error message that is associated with the validation control.</returns>public string ErrorMessage { get; set; }/// <summary>Gets or sets the error message resource name to use in order to look up the <see cref="P:System.ComponentModel.DataAnnotations.ValidationAttribute.ErrorMessageResourceType"></see> property value if validation fails.</summary>/// <returns>The error message resource that is associated with a validation control.</returns>public string ErrorMessageResourceName { get; set; }/// <summary>Gets or sets the resource type to use for error-message lookup if validation fails.</summary>/// <returns>The type of error message that is associated with a validation control.</returns>public Type ErrorMessageResourceType { get; set; }/// <summary>Gets the localized validation error message.</summary>/// <returns>The localized validation error message.</returns>protected string ErrorMessageString { get; }/// <summary>Gets a value that indicates whether the attribute requires validation context.</summary>/// <returns>true if the attribute requires validation context; otherwise, false.</returns>public virtual bool RequiresValidationContext { get; }/// <summary>Applies formatting to an error message, based on the data field where the error occurred.</summary>/// <param name="name">The name to include in the formatted message.</param>/// <returns>An instance of the formatted error message.</returns>public virtual string FormatErrorMessage(string name);/// <summary>Checks whether the specified value is valid with respect to the current validation attribute.</summary>/// <param name="value">The value to validate.</param>/// <param name="validationContext">The context information about the validation operation.</param>/// <returns>An instance of the <see cref="System.ComponentModel.DataAnnotations.ValidationResult"></see> class.</returns>public ValidationResult GetValidationResult(object value,ValidationContext validationContext);/// <summary>Determines whether the specified value of the object is valid.</summary>/// <param name="value">The value of the object to validate.</param>/// <returns>true if the specified value is valid; otherwise, false.</returns>public virtual bool IsValid(object value);/// <summary>Validates the specified value with respect to the current validation attribute.</summary>/// <param name="value">The value to validate.</param>/// <param name="validationContext">The context information about the validation operation.</param>/// <returns>An instance of the <see cref="System.ComponentModel.DataAnnotations.ValidationResult"></see> class.</returns>protected virtual ValidationResult IsValid(object value,ValidationContext validationContext);/// <summary>Validates the specified object.</summary>/// <param name="value">The object to validate.</param>/// <param name="validationContext">The <see cref="T:System.ComponentModel.DataAnnotations.ValidationContext"></see> object that describes the context where the validation checks are performed. This parameter cannot be null.</param>/// <exception cref="T:System.ComponentModel.DataAnnotations.ValidationException">Validation failed.</exception>public void Validate(object value, ValidationContext validationContext);/// <summary>Validates the specified object.</summary>/// <param name="value">The value of the object to validate.</param>/// <param name="name">The name to include in the error message.</param>/// <exception cref="T:System.ComponentModel.DataAnnotations.ValidationException"><paramref name="value">value</paramref> is not valid.</exception>public void Validate(object value, string name);}