ASP.NET Core中如何利用多种方式给Action传参(2)

public class DefaultController : Controller { private readonly AuthorRepository authorRepository = new AuthorRepository(); [HttpGet] [Route("Default/GetAuthor/{authorId:int}")] public IActionResult GetAuthor(int authorId) { var data = authorRepository.GetAuthor(authorId); return Ok(data); } [HttpGet] [Route("Default/GetAuthors/{pageNumber:int}")] public IActionResult GetAuthors([FromQuery (Name = "pageNumber")] int pageNumber = 1) { var data = authorRepository.GetAuthors(pageNumber); return Ok(data); } [HttpGet] [Route("Default/IsCreditCardValid/{creditCardNumber}")] public IActionResult IsCreditCardValid ([FromHeader] string creditCardNumber) { string regexExpression = "^(?:(?<visa>4[0-9]{12}(?:[0-9]{3})?)|" + "(?<mastercard>5[1-5][0-9]{14})|" + "(?<amex>3[47][0-9]{13})|)$"; Regex regex = new Regex(regexExpression); var match = regex.Match(creditCardNumber); return Ok(match.Success); } [HttpPost] [Route("Default/Insert")] public IActionResult Insert([FromBody] Author author) { return Ok(authorRepository.Save(author)); } }

最后,你也可以采用 form 的形式给 Action 传递参数,不过 form 的场景更多的用于 文件上传,要这么做的话,可以利用 IFormFile 去搞定。

到此这篇关于ASP.NET Core中如何利用多种方式给Action传参的文章就介绍到这了,更多相关ASP.NET Core用多种方式给Action传参内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

译文链接:https://www.infoworld.com/article/3568209/how-to-pass-parameters-to-action-methods-in-asp-net-core-mvc.html

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/e2e97201fc01b06a75c192376e1300e6.html