# Day4 WebApi(續) ###### tags: `CORE` ## 1012 webApi=>scaffolidng 的回傳型別, 那要以哪一個為主? - `ActionResult`: `List<Product>` - `IActionResult`: 例如回傳`NotFound()`或是`Ok` ### policy ```csharp= options.AddPolicy("Policy1", builder => { builder.WithOrigins("https://localhost:44328"); }); ``` ### Swagger 1. 安裝套件 `Swashbuckle.AspNetCore` 2. `services.AddSwaggerGen();` 3. 其他參考講義(Day5) 一般存取的網址: `https://localhost:44331/swagger/index.html` 加上Prefix ```csharp= c.RoutePrefix = string.Empty; ``` 網址: `https://localhost:44331/index.html` ```c# app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API v1"); c.RoutePrefix = string.Empty; }); ``` ### 加上`[productsResponseType]` 改變文件內容 ```c# [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ApiController] [Produces("application/json")] ```  ```csharp= - 1.AjaxCall.html的contentType:"application/json" - 2.Web API - Post Action套用路由:[Route("~/api/todoitems/json/")] - 3.Web API - Post Action參數前面不要加任何的[FromForm]或[FromBody] - 4.AjaxCall.html的url改成: https://localhost:5001/api/todoitems/json - 5.JSON.stringify(todoitem) - 6.Cors設定AllowAnyHeader()方法 options.AddDefaultPolicy( builder => { builder.AllowAnyOrigin(); //for CROS - Any Origin builder.WithOrigins("https://localhost:44310"); builder.AllowAnyHeader(); //for POST JSON builder.AllowAnyMethod(); //for DELETE Method }); ``` ## 1014 - `dotnet run --urls "https://localhost:6001;http://localhost:6002"` - `code -r 專案名稱`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up