asp.net-core-webapi

Handling Model Binding Errors when using [FromBody] in .NET Core 2.1

心不动则不痛 提交于 2020-12-25 11:01:48
问题 I am trying to understand how I can intercept and handle model binding errors in .net core. I want to do this: // POST api/values [HttpPost] public void Post([FromBody] Thing value) { if (!ModelState.IsValid) { // Handle Error Here } } Where the Model for "Thing" is: public class Thing { public string Description { get; set; } public int Amount { get; set; } } However if I pass in an invalid amount like: { "description" : "Cats", "amount" : 21.25 } I get an error back like this: {"amount":[

Handling Model Binding Errors when using [FromBody] in .NET Core 2.1

随声附和 提交于 2020-12-25 11:01:29
问题 I am trying to understand how I can intercept and handle model binding errors in .net core. I want to do this: // POST api/values [HttpPost] public void Post([FromBody] Thing value) { if (!ModelState.IsValid) { // Handle Error Here } } Where the Model for "Thing" is: public class Thing { public string Description { get; set; } public int Amount { get; set; } } However if I pass in an invalid amount like: { "description" : "Cats", "amount" : 21.25 } I get an error back like this: {"amount":[

JWT: Changing a user's role after authentication

谁说我不能喝 提交于 2020-12-06 16:58:34
问题 I'm playing with asp.net Core implementing a little WEB API that will be consumed by an iOS/Android app. I'm using JWT tokens to authorize the users. I'm adding a role claim upon user login that determines whether the user is an administrator or a normal user at the time of issuing the JWT, and I'm using the [Authorize(Role = '...')] attribute to make sure that certain endpoints are only available for the respective role(s). This works fine and well so far. The code to create the JWT looks

JWT: Changing a user's role after authentication

不问归期 提交于 2020-12-06 16:55:31
问题 I'm playing with asp.net Core implementing a little WEB API that will be consumed by an iOS/Android app. I'm using JWT tokens to authorize the users. I'm adding a role claim upon user login that determines whether the user is an administrator or a normal user at the time of issuing the JWT, and I'm using the [Authorize(Role = '...')] attribute to make sure that certain endpoints are only available for the respective role(s). This works fine and well so far. The code to create the JWT looks

Response to preflight request doesn't pass access control check: It does not have HTTP ok status. GET working POST PUT DELETE not working

那年仲夏 提交于 2020-12-06 06:43:23
问题 Greetings I have one web application with following architecture: Web api: ASP.net core 2.1 (Windows Authentication) UI: angular 8 UI is able to get data but unable to send data. I mean GET method is working fine but POST, PUT, DELETE options are not working . And all the methods are working using POSTMAN. ERROR is: Access to XMLHttpRequest at 'http://xx.xxx.xxx.xx:xxyy/xxx/xxxxxx/Method' from origin 'http://localhost:xxxx' has been blocked by CORS policy: Response to preflight request doesn

Serilog logging web-api methods, adding context properties inside middleware

不羁岁月 提交于 2020-12-03 07:37:16
问题 I've been struggling to log response body payload data with serilog, logging from middleware. I'm working on WEB API Core application, with swagger added to endpoints, and my goal is to log every endpoint call to a .json file with serilog (both request and response data). For GET requests, body of the response should be logged (added to serilog context as a property), and for POST requests, both body of request and response should be logged. I have created middleware and managed to properly

Serilog logging web-api methods, adding context properties inside middleware

好久不见. 提交于 2020-12-03 07:34:21
问题 I've been struggling to log response body payload data with serilog, logging from middleware. I'm working on WEB API Core application, with swagger added to endpoints, and my goal is to log every endpoint call to a .json file with serilog (both request and response data). For GET requests, body of the response should be logged (added to serilog context as a property), and for POST requests, both body of request and response should be logged. I have created middleware and managed to properly

Hosting ASP.NET Core API in a Windows Forms Application

浪尽此生 提交于 2020-11-28 01:52:47
问题 Background: I am working on a project that involves a WinForms app. The client wants to expose a local-only HTTP server to allow other apps to trigger functionality on a running instance of the WinForms app via a REST API (or similar). The preference is to implement the aforementioned API using ASP.NET Core. My question is thus: How do I structure a project to have both an ASP.NET Core API and a WinForms GUI in the same process? Are there any pitfalls I'd have to be wary of? 回答1: Hosting ASP