Equivalent of HttpResponseException/IHttpActionResponse for .net Core webapi 2 (not mvc)

IActionResult is the equivalent of IHttpActionResult, as you’ve suggested. This is part of the consolidation of what was known as MVC and Web API in ASP.NET Core MVC. As for HttpResponseException, this has been completely removed in ASP.NET Core. There’s an interesting issue around this on GitHub, where David Fowler gives an explanation as to … Read more

Mock HttpRequest in ASP.NET Core Controller

When creating an instance of the controller under test, make sure to assign a HttpContext that contains the required dependencies for the test to be exercised to completion. You could try mocking a HttpContext and providing that to the controller or just use DefaultHttpContext provided by the framework //Arrange var mockedAccessor = new Mock<IAccessor>(); //…setup … Read more

Actions require unique method/path combination for Swagger

You can resolve it as follows: services.AddSwaggerGen (c => { other configs; c.ResolveConflictingActions (apiDescriptions => apiDescriptions.First ()); }); //in the Startup.cs class in the ConfigureServices method or you can put routes to differentiate your methods, for example: [HttpGet(“~/getsomething”)] [HttpGet(“~/getothersomething”)]

What is the best practice in EF Core for using parallel async calls with an Injected DbContext?

Using any context.XyzAsync() method is only useful if you either await the called method or return control to a calling thread that’s doesn’t have context in its scope. A DbContext instance isn’t thread-safe: you should never ever use it in parallel threads. Which means, just for sure, never use it in multiple threads anyway, even … Read more

ASP.NET Core Web API Logging from a Static Class

Solution is to have a static reference to the LoggerFactory in a utility static class initialized on startup: /// <summary> /// Shared logger /// </summary> internal static class ApplicationLogging { internal static ILoggerFactory LoggerFactory { get; set; }// = new LoggerFactory(); internal static ILogger CreateLogger<T>() => LoggerFactory.CreateLogger<T>(); internal static ILogger CreateLogger(string categoryName) => LoggerFactory.CreateLogger(categoryName); } … 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 do I resolve the issue the request matched multiple endpoints in .Net Core Web Api

What you’re trying to do is impossible because the actions are dynamically activated. The request data (such as a query string) cannot be bound until the framework knows the action signature. It can’t know the action signature until it follows the route. Therefore, you can’t make routing dependent on things the framework doesn’t even know … Read more