积极答复者
swagger 加载model 的问题

问题
-
请教一下大家,我按照这里的
https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-1.1&tabs=visual-studio%2Cvisual-studio-xml
配置,可以运行了swagger。但有一点,这个模型(model),我在数据库里新增两个表后,反向进了model里面,但swagger依然是一个。怎么样可以让它自动识别出来?
- 已编辑 Sam-'''- 2018年9月14日 10:30 错别字
答案
-
你好,
据我所知,如果你要想自动在swagger里面添加model,你需要在你的web api里面使用这个类。
比如使用类当参数或者返回值的类型就是这个参数。
这样就可以显示了。
具体你可以参照如下例子:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using static CoreSwiger.Controllers.ValuesController; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 namespace CoreSwiger.Controllers { [Route("api/[controller]")] public class TestController : Controller { [HttpPost] public Student GetA( Student s1) { Student S1 = new Student() { Id = 1 }; return S1; } [HttpGet] public Person GetPer(string id) { Person S1 = new Person() { Id = 1 }; return S1; } } }
图片:
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已标记为答案 Sam-'''- 2018年9月18日 9:21
全部回复
-
你好,
据我所知,如果你要想自动在swagger里面添加model,你需要在你的web api里面使用这个类。
比如使用类当参数或者返回值的类型就是这个参数。
这样就可以显示了。
具体你可以参照如下例子:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using static CoreSwiger.Controllers.ValuesController; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 namespace CoreSwiger.Controllers { [Route("api/[controller]")] public class TestController : Controller { [HttpPost] public Student GetA( Student s1) { Student S1 = new Student() { Id = 1 }; return S1; } [HttpGet] public Person GetPer(string id) { Person S1 = new Person() { Id = 1 }; return S1; } } }
图片:
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已标记为答案 Sam-'''- 2018年9月18日 9:21
-
你好,
据我所知使用IActionResult的主要优点是你可以返回错误/状态代码或重定向/资源URL。
具体例子如下
public IActionResult Get(integer id) { var user = db.Users.Where(u => u.UserId = id).FirstOrDefault(); if(user == null) { // Returns HttpCode 404 return NotFound(); } // returns HttpCode 200 return ObjectOk(user); }
或
public IActionResult Create(User user) { if(!ModelState.IsValid) { // returns HttpCode 400 return BadRequest(ModelState); } db.Users.Add(user); db.SaveChanges(); // returns HttpCode 201 return CreatedAtActionResult("User", "Get", new { id = user.Id} ); }
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.