溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

asp.net core學習筆記

發布時間:2020-07-04 22:32:21 來源:網絡 閱讀:1246 作者:志強1224 欄目:編程語言

控制器Controller

命名以Controller結尾

public class TestController : Controller{

public IActionResult SayHelo(){

return Content(Hello);

}

}

使用ControllerAttribute標注

[Controller]

public class Test : Controller{

public IActionResult SayHelo(){

return Content(Hello);

}

}

使用NonController標注該類不是控制器

[NonController]

public class TestController : Controller{

public IActionResult SayHelo(){

return Content(Hello);

}

}

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

域名/{Controller}/{Action}

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

接受數據

數據形式

QueryString

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

表單Form

客戶端Cookie

服務端會話Session

頭數據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對象

Session.set設置

Session.Remove移除

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);//?代表可空類型數據

 

數據綁定

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

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

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

Form表單名稱與參數一致

查詢字符串或表單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動態結果接口

具體實現

JsonResult:返回JSON結果數據

RedirectResult:跳轉新網址

FileResult:返回文件

ViewResult:返回視圖頁面

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

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

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

自動執行,無需手動調用

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

 

ViewImport

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

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

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

自動執行,無需手動調用

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

引入方式

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

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


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女