Asp.Netcore学習ノート(Web Api)
5324 ワード
更新:2019-06-03
web apiがjsonを返す場合はデフォルトで属性PascalCaseをcamelCaseにするのがベストですよ.
気に入らなければ修正してもいいです
しかし、これはodataのresponseには影響しませんよ.odataは自動的にcamelCaseしません.
asp.Netcoreは前のwebapiとmvcを結合した.
mvcはapiである.
しかしその後、apiには確かにユニークなところがあることに気づき、補助の方法を開いた.
引き継いだのはMVCでよく使われるControllerBaseではなくControllerBaseである.ControllerはControllerBaseを継承します
[ApiController],ラベルApiControllerを使用すると自動model validチェック,自動binding FromBody,FromQueryなどがオンになりますが,FromFormはやはり自分で書くようにしましょう(デフォルトapiはjsonですか),知能が心配なら完全に自分で書くこともできます.
あるいは知能をオフにします Add the following code in Startup.ConfigureServices after
[HttpPost("nextSegment")]http methodタグで、get、post、put、delete、routeなどの各作り方を簡単に書くことができます.
[producesResponseType]このラベルの主な機能はdocumentを容易にするためであり、ActionResult汎用型に合わせて、正常な場合の戻りを簡単に表すことができ、残りの異常はProducesResponseTypeで表すことができる.
普通は404、400は他にないでしょう.
転載先:https://www.cnblogs.com/keatkeat/p/9297672.html
web apiがjsonを返す場合はデフォルトで属性PascalCaseをcamelCaseにするのがベストですよ.
気に入らなければ修正してもいいです
services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver())
しかし、これはodataのresponseには影響しませんよ.odataは自動的にcamelCaseしません.
asp.Netcoreは前のwebapiとmvcを結合した.
mvcはapiである.
しかしその後、apiには確かにユニークなところがあることに気づき、補助の方法を開いた.
namespace Project.Controllers
{
public class PostForm
{
[Required]
public IFormFile file { get; set; }
}
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private DB Db { get; set; }
public ProductsController(DB db)
{
Db = db;
}
[HttpGet]
public ActionResult> Get()
{
return Ok(Db.Products);
}
[HttpGet("{Id}")]
[ProducesResponseType(400)]
[ProducesResponseType(404)]
public ActionResult GetById(string Id,[Required] string code)
{
return NotFound();
}
[HttpPost]
[ProducesResponseType(400)]
public async Task> Post(Product product)
{
Db.Add(product);
await Db.SaveChangesAsync();
return Ok(product);
}
[HttpPost("upload")]
[ProducesResponseType(400)]
public ActionResult<string> Upload([FromForm] PostForm form)
{
return Ok("filename");
}
}
}
引き継いだのはMVCでよく使われるControllerBaseではなくControllerBaseである.ControllerはControllerBaseを継承します
[ApiController],ラベルApiControllerを使用すると自動model validチェック,自動binding FromBody,FromQueryなどがオンになりますが,FromFormはやはり自分で書くようにしましょう(デフォルトapiはjsonですか),知能が心配なら完全に自分で書くこともできます.
あるいは知能をオフにします Add the following code in Startup.ConfigureServices after
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
: services.Configure(options =>
{
options.SuppressConsumesConstraintForFormFileParameters = true;
options.SuppressInferBindingSourcesForParameters = true;
options.SuppressModelStateInvalidFilter = true;
});
[HttpPost("nextSegment")]http methodタグで、get、post、put、delete、routeなどの各作り方を簡単に書くことができます.
[producesResponseType]このラベルの主な機能はdocumentを容易にするためであり、ActionResult汎用型に合わせて、正常な場合の戻りを簡単に表すことができ、残りの異常はProducesResponseTypeで表すことができる.
普通は404、400は他にないでしょう.
転載先:https://www.cnblogs.com/keatkeat/p/9297672.html