激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - 詳解如何在ASP.NET Core中使用Route特性

詳解如何在ASP.NET Core中使用Route特性

2021-12-08 15:03一線碼農 ASP.NET教程

這篇文章主要介紹了如何在ASP.NET Core中使用Route特性,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

 ASP.NET Core 中的 Route 中間件的職責在于將 request 匹配到各自 Route 處理程序上,Route 分兩種:基于約定 和 基本特性 模式。

基于約定 模式的Route采用集中化的方式,而 基于特性 的方式允許你在 Action 或者 Controller 上單獨定義,到底采用哪一種可以基于你自己的應用場景,本篇就來討論如何使用 基于特性 模式。

創建 Controller 類

創建一個 DefaultController 類,新增如下代碼。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class DefaultController : Controller
{
 [Route("")]
 [Route("Default")]
 [Route("Default/Index")]
 public ActionResult Index()
 {
  return new EmptyResult();
 }
 [Route("Default/GetRecordsById/{id}")]
 public ActionResult GetRecordsById(int id)
 {
  string str = string.Format
  ("The id passed as parameter is: {0}", id);
  return Ok(str);
 }
}

Controller 級別定義 Route 特性

Route特性可用于 Controller 和 Action 級別,值得注意的是,如果應到到前者,那么 Controller 下的所有 Action 都受這個 Route 管控。

如果你仔細觀察上面的 DefaultController 類代碼,你會發現兩個 Action 方法的 Route 路徑都有 Default 前綴,這就不優雅了,優化方式就是把 Route 路徑中的 Default 提取到 Controller 級別,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[Route("Default")]
public class DefaultController : Controller
{
 [Route("")]
 [Route("Index")]
 public ActionResult Index()
 {
  return new EmptyResult();
 }
 [HttpGet]
 [Route("GetRecordsById/{id}")]
 public ActionResult GetRecordsById(int id)
 {
  string str = string.Format("The id passed as parameter is: {0}", id);
  return Ok(str);
 }
}

可以看出當 Controller 和 Action 級別都被 Route 打上標記之后,Asp.Net Core 中的 Route 引擎會自動將兩者拼接起來,當然更簡單粗暴的做法就是在 Controller 上使用 RoutePrefix 特性,如下代碼所示:

?
1
2
3
4
5
[RoutePrefix("services")]
public class HomeController : Controller
{
 //Action methods
}

Action 級別定義 Route 特性

參考剛才的 DefaultController 類,我在 Index 方法上面定義了三個 Route 特性,這就意味著下面三種 Route 都可以訪問到 Index() 方法,如下代碼所示:

http://localhost:11277
http://localhost:11277/home
http://localhost:11277/home/index

常常在 基于約定 模式的Route中,它的 Route template 會有一些對參數的約定,比如下面的代碼:

?
1
2
3
4
5
6
app.UseEndpoints(endpoints =>
{
 endpoints.MapControllerRoute(
  name: "default",
  pattern: "{controller=Home}/{action=Index}/{id?}");
});

同樣 基于特性 模式的 Route 也是可以使用參數模式的,比如文章之前的 DefaultController.GetRecordsById 就是的,值得注意的是模板中的 {id} 表示可接收任何參數,如 string,int 等等,如果你想限定為 int 的話,也是可以實現的。

使用 Route 約束

Route 約束 就是 Controller 前的一個防火墻,他會踢掉一些不合規范的 Action 請求,比如說:你要求某個 Action 接收的參數必須是 int,那在 Route 模板中定義的語法格式一定是這樣的 {parameter:constraint},如下代碼所示:

?
1
2
3
4
5
6
[Route("Default/GetRecordsById/{id:int}")]
public ActionResult GetRecordsById(int id)
{
 string str = string.Format("The id passed as parameter is: {0}", id);
 return Ok(str);
}

在 Route 中使用可選參數

你也可以在 Route Template 上指定可選參數,意味著這個參數可傳可不傳,格式如下:

?
1
[Route("Sales/GetSalesByRegionId/{id?}")]

有一點非常重要,當你使用了 Route特性 之后,其實 Controller 或者 Action 的名字就不再重要了,因為 Route處理引擎 已經不再將其作為參考選項,下面的代碼片段展示了如何在 Action 方法上變更 Route template 格式。

?
1
2
3
4
5
6
[Route("Home/GetRecordsById/{id:int}")]
public ActionResult GetRecordsById(int id)
{
 string str = string.Format("The id passed as parameter is: {0}", id);
 return Ok(str);
}

接下來可以直接使用如下地址訪問 GetRecordsById 方法。

http://localhost:11277/home/GetRecordsById/1

對 Action 中的參數使用多個約束

真實場景中你不僅要求 id 必須是整數,還要求必須有一定意義,比如說最小值為1,對這種有 多重約束 的需求如何去實現呢? 請看下面代碼。

?
1
2
3
4
5
6
[Route("Default/GetRecordsById/{id:int:min(1)}")]
public ActionResult GetRecordsById(int id)
{
 string str = string.Format("The id passed as parameter is: {0}", id);
 return Ok(str);
}

常使用的 Route 約束

  • int 限定為 int 類型
  • max/min 限定 int 的最大數和最小數
  • minlength 限定 string 的最小長度
  • regex 限定符合的正則

創建自定義的 Route 約束

如果上面的一些約束不滿足你的要求,你完全可以為你的場景深度定制,做法就是使用 IRouteConstraint 接口并實現它的 Match 方法即可,如下代碼所示:

?
1
2
3
4
5
6
7
8
9
public class CustomRouteConstraint : IRouteConstraint
{
 public bool Match(HttpContext httpContext, IRouter route,
 string routeKey,
 RouteValueDictionary values, RouteDirection routeDirection)
 {
  throw new NotImplementedException();
 }
}

在 Controller 上使用 token 占位符

所謂的 token 占位符 就是具有一些特定含義的占位符號,比如說:[action], [area] 和 [controller],分別表示用你真實的 Controller 和 Action 去替換,下面的代碼展示了如何使用這種模式去實現。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Route("[controller]/[action]")]
public class HomeController : Controller
{
 private readonly ILogger<HomeController> _logger;
 public HomeController(ILogger<HomeController> logger)
 {
  _logger = logger;
 }
 public IActionResult Index()
 {
  return View();
 }
 //Other action methods
}

整體來看,基于特性 的 Route 給了你更多的操控權限,靈活的 Route Template 配置實現了 Controller 和 Action 的解耦,當然這里也不是說 基于約定 的Route 不好,畢竟人家是 Global 級別的,真實場景下兩者更多的是混著用。

譯文鏈接:https://www.infoworld.com/article/3569369/how-to-use-attribute-routing-in-aspnet-core.html

到此這篇關于如何在ASP.NET Core中使用Route特性的文章就介紹到這了,更多相關ASP.NET Core Route特性內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://segmentfault.com/a/1190000039068171

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美一区二区三区不卡免费观看 | 特级a欧美做爰片毛片 | 免费观看一级淫片 | 黄色一级片毛片 | 日日草夜夜| 天天黄色片 | 成人午夜在线免费视频 | 精品一区二区视频在线观看 | 久久99精品久久久久久园产越南 | 91av在线免费播放 | 精品久久久一二三区播放播放播放视频 | 免费看日韩av| 精品一区二区在线观看视频 | 久产久精品 | 成人午夜在线免费观看 | 27xxoo无遮挡动态视频 | 中文字幕 亚洲一区 | 福利免费视频 | cosplay裸体福利写真 | 久久久青 | 18视频在线观看娇喘 | 涩涩屋av| 国产一级淫片a级aaa | 精品国产一区在线观看 | 久国产 | 亚洲精品久久久久久久久久 | 久久免费综合视频 | av电影网站在线 | 亚洲国产网站 | 国产精品久久久久久久久久免 | 黄污网站在线观看 | 中文字幕欧美专区 | 香蕉在线看 | 日本精品婷婷久久爽一下 | 久久精品国产99国产精品亚洲 | 成年人在线免费播放视频 | 久久久久在线 | 视频一区 中文字幕 | 超碰97最新 | 黄色av网站免费 | 青青国产在线视频 |