Questions tagged [asp.net-core-webapi]

Questions about ASP.NET Core web APIs and web apps that are not dependent on MVC Views or Razor Pages

5650 questions
44
votes
21 answers

The target process exited without raising CoreCLR started event error with .NET Core 2.2

I want to debug empty WebApi Project based on .NET Core 2.2. I installed Core 2.2 SDK x86 and changed target framework to 2.2: netcoreapp2.2
44
votes
9 answers

Enable OPTIONS header for CORS on .NET Core Web API

I solved this problem after not finding the solution on Stackoverflow, so I am sharing my problem here and the solution in an answer. After enabling a cross domain policy in my .NET Core Web Api application with AddCors, it still does not work from…
Niels Brinch
  • 2,819
  • 7
  • 34
  • 57
42
votes
2 answers

Mock IHttpContextAccessor in Unit Tests

I have a method to get header value using IHttpContextAccessor public class HeaderConfiguration : IHeaderConfiguration { public HeaderConfiguration() { } public string GetTenantId(IHttpContextAccessor httpContextAccessor) { …
superninja
  • 1,984
  • 5
  • 19
  • 39
38
votes
2 answers

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

I have a .NET Core 1.1 API with EF Core 1.1 and using Microsoft's vanilla setup of using Dependency Injection to provide the DbContext to my services. (Reference:…
38
votes
1 answer

How to implement a "pure" ASP.NET Core Web API by using AddMvcCore()

I've seen a lot of ASP.NET Core Web API projects that use the default AddMvc() service without the realizing that using AddMvcCore() is a superior option due to the control over services. How exactly do you implement an ASP.NET Core Web API by using…
Svek
  • 10,726
  • 4
  • 32
  • 63
37
votes
6 answers

ASP.NET Core Authorize attribute not working with JWT

I want to implement JWT-based security in ASP.Net Core. All I want it to do, for now, is to read bearer tokens in the Authorization header and validate them against my criteria. I don't need (and don't want) to include ASP.Net Identity. In fact, I'm…
Andrew Williamson
  • 6,465
  • 2
  • 30
  • 48
36
votes
6 answers

JWT Authentication and Swagger with .NET Core 3.0

I am developing some Web API with .NET Core 3.0 and want to integrate it with SwashBuckle.Swagger. It is working fine, but when I add JWT authentication, it does not work as I expect. To do that, I added the code below: services.AddSwaggerGen(c => …
Mehrdad Babaki
  • 7,301
  • 12
  • 39
  • 59
36
votes
4 answers

How can i configure JSON format indents in ASP.NET Core Web API

How can i configure ASP.NET Core Web Api controller to return pretty formatted json for Development enviroment only? By default it returns something like: {"id":1,"code":"4315"} I would like to have indents in the response for readability: { …
Mariusz Jamro
  • 27,234
  • 22
  • 104
  • 144
35
votes
1 answer

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

When I am reading about webapi for responding to requests and handling errors everything is based around: IHttpActionResult HttpResponseException But when you create a .net core webapi project these are not available. I can find IActionResult,…
Neil Walker
  • 5,334
  • 10
  • 51
  • 75
35
votes
1 answer

Asp Net Core Web Push Notifications

Main goal is to add to site ability to send web notification to pop up a system notification to alert the user using Html5 Push API and service workers. Not using SignalR which only can run client scripts while site is opened. Also should be ability…
aleha
  • 7,140
  • 2
  • 34
  • 41
34
votes
2 answers

Should I always add CancellationToken to my controller actions?

Is this a good practice to always add CancellationToken in my actions no matter if operation is long or not? I'm currently adding it to every action and I don't know if it's right or wrong. [ApiController] [Route("api/[controller]")] public class…
34
votes
1 answer

Using Dependency Injection with .NET Core Class Library (.NET Standard)

I have gone through the link: https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/dependency-injection and learnt that how I can use dependency injection for Web API. As mentioned in the above link I can use Startup (Startup.cs) class for…
34
votes
2 answers

No Individual User Accounts auth option in ASP.NET Core Web API template

I am a bit confused as to why there is no Individual User Accounts authentication option in the latest ASP.NET Core Web API template. Is it still possible to implement individual user accounts the way that the MVC template does or would it not make…
34
votes
2 answers

Getting Scope Validating error in Identity Server 4 using JavaScript Client in asp.net core

I am getting the below error while making a request to my Identity Server application from my Javascript Client Application. fail: IdentityServer4.Validation.ScopeValidator[0] Invalid scope: openid I have made sure I add the scope in my Identity…
maxspan
  • 10,656
  • 12
  • 61
  • 88
33
votes
4 answers

ASP.NET Core Web API Logging from a Static Class

I'm logging just fine using dependency injection on my controllers, now I need to log something from a static class. How can I log from a static class? I can't use dependency injection because it's static and I can't just pass in an existing logger…