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
299
votes
4 answers

RS256 vs HS256: What's the difference?

I'm using Auth0 to handle authentication in my web app. I'm using ASP.NET Core v1.0.0 and Angular 2 rc5 and I don't know much about authentication/security in general. In the Auth0 docs for ASP.NET Core Web Api, there are two choices for the JWT…
Rico Kahler
  • 12,583
  • 9
  • 41
  • 63
179
votes
12 answers

ASP.NET Core return JSON with status code

I'm looking for the correct way to return JSON with a HTTP status code in my .NET Core Web API controller. I use to use it like this: public IHttpActionResult GetResourceData() { return this.Content(HttpStatusCode.OK, new { response =…
Rossco
  • 2,933
  • 2
  • 20
  • 34
153
votes
3 answers

Return file in ASP.Net Core Web API

Problem I want to return a file in my ASP.Net Web API Controller, but all my approaches return the HttpResponseMessage as JSON. Code so far public async Task DownloadAsync(string id) { var response = new…
Jan Kruse
  • 1,915
  • 2
  • 11
  • 15
142
votes
5 answers

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

I want the example controller below to return a status code 418 with no contents. Setting the status code is easy enough but then it seems like there is something that needs to be done to signal the end of the request. In MVC prior to ASP.NET Core…
RonC
  • 23,270
  • 13
  • 67
  • 104
81
votes
4 answers

Returning a 404 from an explicitly typed ASP.NET Core API controller (not IActionResult)

ASP.NET Core API controllers typically return explicit types (and do so by default if you create a new project), something like: [Route("api/[controller]")] public class ThingsController : Controller { // GET api/things [HttpGet] public…
Keith
  • 133,927
  • 68
  • 273
  • 391
75
votes
6 answers

How to add custom header to ASP.NET Core Web API response

I am porting my API from Web API 2 to ASP.NET Core Web API. I used to be able to add a custom header in the following manner: HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); response.Headers.Add("X-Total-Count",…
JDawg
  • 5,072
  • 2
  • 20
  • 26
71
votes
6 answers

Unit testing controller methods which return IActionResult

I'm in the process of building an ASP.NET Core WebAPI and I'm attempting to write unit tests for the controllers. Most examples I've found are from the older WebAPI/WebAPI2 platforms and don't seem to correlate with the new Core controllers. My…
68
votes
2 answers

MediatR when and why I should use it? vs 2017 webapi

It might have been asked before but I cannot find even in the official site why I should use MediatR and what problems it solves? Is it because I can pass a single object in my constructor rather than a multitude of Interfaces? Is it a…
developer9969
  • 2,965
  • 3
  • 26
  • 58
67
votes
4 answers

Return jpeg image from Asp.Net Core WebAPI

Using asp.net core web api, I want to have my controller action method to return an jpeg image stream. In my current implementation, browser displays only a json string. My expectation is to see the image in the browser. While debugging using…
hannes neukermans
  • 8,470
  • 5
  • 30
  • 44
66
votes
8 answers

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

I'm developing an ASP.Net Core web application where I need to create a kind of "authentication proxy" to another (external) web service. What I mean by authentication proxy is that I will receive requests through a specific path of my web app and…
Gimly
  • 5,341
  • 3
  • 38
  • 69
60
votes
12 answers

The configuration file 'appsettings.json' was not found and is not optional

The Azure error is: .Net Core: Application startup exception: System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found and is not optional. So this is a bit vague. I can't seem to nail this down. I'm trying to…
59
votes
7 answers

How do I get current user in .NET Core Web API (from JWT Token)

After a lot of struggling (and a lot of tuturials, guides, etc) I managed to setup a small .NET Core REST Web API with an Auth Controller issuing JWT tokens when stored username and password are valid. The token stores the user id as sub claim. I…
monty
  • 5,629
  • 11
  • 44
  • 74
54
votes
8 answers

ASP.NET Core with IIS - HTTP Verb Not Allowed

We have an ASP.NET Core 2.0 web site that also presents a couple of simple Web API methods for UI enhancement purposes. The Web API calls work as expected when running locally under IIS Express, but when we deploy to our IIS 8.5 production web…
Neilski
  • 4,043
  • 5
  • 35
  • 69
51
votes
8 answers

The JSON value could not be converted to System.Int32

I want to send data of object to my Web API. The API accepts a parameter of class, which properties are type of int and string. This is my class: public class deneme { public int ID { get; set; } public int sayi { get; set; } public int…
ali suleymanli
  • 693
  • 1
  • 6
  • 12
49
votes
11 answers

ASP.NET Core 2.0 JWT Validation fails with `Authorization failed for user: (null)` error

I'm using ASP.NET Core 2.0 application (Web API) as a JWT issuer to generate a token consumable by a mobile app. Unfortunately, this token couldn't be validated by one controller while can be validated by another (using the same validation setting…
1
2 3
99 100