Async provider in .NET Core DI

Although it is theoretically possible to use async/await during object resolution, it doesn’t make much sense when resolving dependencies, because: Constructors can’t be asynchronous, and Construction of object graphs should be simple, reliable and fast This means that everything that involves I/O should be postponed until after the object graph has been constructed. So instead … Read more

How do I customize ASP.Net Core model binding errors?

It’s worth mentioning that ASP.NET Core 2.1 added the [ApiController] attribute, which among other things, automatically handles model validation errors by returning a BadRequestObjectResult with ModelState passed in. In other words, if you decorate your controllers with that attribute, you no longer need to do the if (!ModelState.IsValid) check. Additionally, the functionality is also extensible. … Read more

ASP.NET Core JWT mapping role claims to ClaimsIdentity

You need get valid claims when generating JWT. Here is example code: Login logic: [HttpPost] [AllowAnonymous] public async Task<IActionResult> Login([FromBody] ApplicationUser applicationUser) { var result = await _signInManager.PasswordSignInAsync(applicationUser.UserName, applicationUser.Password, true, false); if(result.Succeeded) { var user = await _userManager.FindByNameAsync(applicationUser.UserName); // Get valid claims and pass them into JWT var claims = await GetValidClaims(user); // Create the … Read more

Uploading and Downloading large files in ASP.NET Core 3.1?

If you have files that large, never use byte[] or MemoryStream in your code. Only operate on streams if you download/upload files. You have a couple of options: If you control both client and server, consider using something like tus. There are both client- and server-implementations for .NET. This would probably the easiest and most … Read more

Creating a proxy to another web api with Asp.net core

If anyone is interested, I took the Microsoft.AspNetCore.Proxy code and made it a little better with middleware. Check it out here: https://github.com/twitchax/AspNetCore.Proxy. NuGet here: https://www.nuget.org/packages/AspNetCore.Proxy/. Microsoft archived the other one mentioned in this post, and I plan on responding to any issues on this project. Basically, it makes reverse proxying another web server a lot … Read more

Enable OPTIONS header for CORS on .NET Core Web API

Add a middleware class to your project to handle the OPTIONS verb. using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Hosting; namespace Web.Middlewares { public class OptionsMiddleware { private readonly RequestDelegate _next; public OptionsMiddleware(RequestDelegate next) { _next = next; } public Task Invoke(HttpContext context) { return BeginInvoke(context); } private Task BeginInvoke(HttpContext context) { if (context.Request.Method == … Read more

How to return a specific status code and no contents from Controller?

this.HttpContext.Response.StatusCode = 418; // I’m a teapot How to end the request? Try other solution, just: return StatusCode(418); You could use StatusCode(???) to return any HTTP status code. Also, you can use dedicated results: Success: return Ok() ← Http status code 200 return Created() ← Http status code 201 return NoContent(); ← Http status code … Read more