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

您大概感乐趣的文章:

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

转载注明出处:https://www.heiqu.com/wsdxyd.html