1. 程式人生 > >asp.net core學習筆記

asp.net core學習筆記

left one borde 表單 服務註冊 strong ide turn mod

控制器Controller

l 命名以Controller結尾

public class TestController : Controller{

public IActionResult SayHelo(){

return Content(Hello);

}

}

l 使用ControllerAttribute標註

[Controller]

public class Test : Controller{

public IActionResult SayHelo(){

return Content(Hello);

}

}

l 使用NonController標註該類不是控制器

[NonController]

public class TestController : Controller{

public IActionResult SayHelo(){

return Content(Hello);

}

}

訪問方式(默認路由規則)

域名/{Controller}/{Action}

域名/{控制器類}/{方法}

接受數據

數據形式

l QueryString

www.jqstu.com/test/sayhello?key1=value1&key2=value2

l 表單Form

l 客戶端Cookie

l 服務端會話Session

l 頭數據Header

HttpRequest是用戶請求的對象

提供獲取請求數據的屬性(Cookie Headers Query Forms)

string value1=Request.Query[

key1];

string name=Request.Form[name];

string cookiesname=Request.Cookies[name];

string cookiesname=Request.Cookies[name];

return Content(Hello+value1);

HttpContext用戶上下文

提供Session熟悉獲取Session對象

l Session.set設置

l Session.Remove移除

l Session.TryGetValue獲取數據

HttpContext.Session.SetString(name,jqstu);

HttpContext.Session.Remove(

name);

string name = HttpContext.Session.GetString(name);

HttpContext.Session.SetInt32(age,32);

int ? age = HttpContext.Session.GetInt32(age);//?代表可空類型數據

數據綁定

把用戶請求的數據綁定在控制方法的參數上

綁定規則是請求數據名稱和參數名稱一致

l 如果查詢字符串key名稱跟參數一致

l Form表單名稱與參數一致

l 查詢字符串或表單key的名稱和類屬性名稱保持一致

支持簡單類型

public IActionResult SayHelo(string name){

return Content(Hello+name);

}

自定義類

public IActionResult SayHelo(TestModel model){

return Content(Hello+model.name);

}

public class TestModel(){

Public string Name{get; set;}

}

常見的特性

假如key同時存在不同的數據源,這時候就要運用特性的方式去綁定數據

特性

數據源

FormHeaderAttribute

Headers

FormRouteAttribute

路由數據

FormBodyAttribute

請求體

FormFromAttribute

表單數據

FormQueryAttribute

查詢字符串

FormServicesAttribute

服務註冊

<form method =post action =/test/postsay?name=zhiqiang>

<input type =text name=name/ >

<input type=submit value=提交/>

<inut type=button value=ajax提交數據 onclick =postheader()/>

</form>

<script type=text/javascript>

function postheader(){

$.ajax({

url:/test/ajaxsay?r=+Math.random(),

beforeSend:function(xhr){

Xhr.setRequestHeader(name,jqstu);

},

type:get,

Success:function(data){

alert(data)

}

});

}

</script>

public IActionResult postsay([FormQuery] string name){

return Content(Hello+name);

}

public IActionResult ajaxsay([FormHeader] string name){

return Content(Hello+name);

}

視圖V

IActionResult動態結果接口

具體實現

l JsonResult:返回JSON結果數據

l RedirectResult:跳轉新網址

l FileResult:返回文件

l ViewResult:返回視圖頁面

l ContentResult:文本內容

返回結果形式的統稱

類型

實例化對象

封裝方法

JSON結果

JsonResult

Json(object)

跳轉

RedirectResult

Redirect(url)

文件

FileResult

File()

視圖

ViewResult

View

文本

ContentResult

Content()

JSON數據輸出

public IActionResult ReturnJson(){

//JsonResult res = new JsonRult( new {name =jqstu});

//return res ;

return Json( new {name =jqstu} );

}

View視圖輸出

public IActionResult ShowView(){

return view();//默認生成/views/Test/showview.cshtml

return view(~/Views/Test/Index.cshtml);

}

異步動作結果,在特定環境可以提供程序的性能

特定的網絡請求,文件,數據庫等等涉及到IO操作使用一下定義方法

public Task<IActionResult > list(){

}

數據傳遞

ViewData

ViewBag

鍵值對

動態類型

索引器

ViewData的封裝

支持任意類型

動態屬性

ViewStart

l _ViewStart.cshtml命名,固定命名,不能更換

l 一般放在視圖所在目錄的根目錄下

l 自動執行,無需手動調用

l 不要在ViewStart中做大量的業務操作

ViewImport

l _ViewImports.cshtml命名,固定命名,不能更換

l 只做引入操作(全局命名空間引入)

l 一般放在視圖所在目錄的根目錄下

l 自動執行,無需手動調用

l 不要在ViewStart中做大量的業務操作

引入方式

視圖中是可以使用@using關鍵字引入所需命名空間

通過ViewImport做全局性的命名空間引入,減少頁面代碼量


asp.net core學習筆記