Questions tagged [asp.net-web-api2]

ASP.NET Web API 2 is a framework for building HTTP services for clients like browsers and mobile devices. It is based on the Microsoft .NET Framework and an ideal choice for building RESTful services.

Web API 2 now includes annotation-based routing.

Developers can assign custom paths to Methods in Web API 2 Controllers. This greatly extends the versatility of Web API, as the original was limited to having 4 methods for each Controller (Get, Post, Put, Delete).

7629 questions
36
votes
3 answers

Web API 2 OWIN Bearer Token purpose of cookie?

I am trying to understand the new OWIN Bearer Token authentication process in the Single Page App template in MVC 5. Please correct me if I'm wrong, for the OAuth password client authentication flow, Bearer Token authentication works by checking…
gavin
  • 1,167
  • 1
  • 10
  • 16
35
votes
2 answers

Returning IHttpActionResult vs IEnumerable vs IQueryable

In ASP.NET Web API 2, what is the difference among the following? public async Task> GetMyItems() { //... code ..., var myItems = await ... return myItems; } and public async Task> GetMyItems() { …
Adam Szabo
  • 10,892
  • 17
  • 58
  • 99
34
votes
1 answer

How to return a PDF from a Web API application

I have a Web API project that is running on a server. It is supposed to return PDFs from two different kinds of sources: an actual portable document file (PDF), and a base64 string stored in a database. The trouble I'm having is sending the document…
Doctor93
  • 355
  • 1
  • 3
  • 10
34
votes
4 answers

Apply [Authorize] attribute implicitly to all Web API controllers

My application is setup where all requests except login must be 'authorized' using the authorization attribute in Web API. E.g. [Authorize] [HttpGet, Route("api/account/profile")] public ApplicationUser Profile() { return userModel; …
amcdnl
  • 7,766
  • 12
  • 59
  • 88
33
votes
4 answers

Post byte array to Web API server using HttpClient

I want to post this data to Web API server: public sealed class SomePostRequest { public int Id { get; set; } public byte[] Content { get; set; } } Using this code for server: [Route("Incoming")] [ValidateModel] public async…
Dennis
  • 34,925
  • 9
  • 72
  • 134
32
votes
3 answers

Enable CORS for Web Api 2 and OWIN token authentication

I have an ASP.NET MVC 5 webproject (localhost:81) that calls functions from my WebApi 2 project (localhost:82) using Knockoutjs, to make the communication between the two projects I enable CORS. Everything works so far until I tried to implement…
Sam
  • 1,153
  • 3
  • 16
  • 38
32
votes
12 answers

How to use Swagger as Welcome Page of IAppBuilder in WebAPI

I try to use Swagger with Microsoft WebAPI 2. For the moment, I've the following call in a method. appBuilder .ConfigureOAuth() .UseWebApi(configuration) .UseWelcomePage(); If I want to use Swagger, I must use this url…
Philippe Matray
  • 1,471
  • 1
  • 13
  • 22
32
votes
2 answers

Stop displaying entire stack trace in WebAPI

When an unexpected error occurs in WebAPI the user sees the entire stack trace. I believe that showing the entire stack trace is not safe. What is the default behaviour to stop showing the entire trace to my users? Just a friendly message like…
31
votes
6 answers

Add a custom response header in ApiController

Until now, I had a GET method that looked like the following: protected override async Task GetAll(QueryData query) { // ... Some operations //LINQ Expression based on the query parameters Expression
Matias Cicero
  • 21,834
  • 10
  • 67
  • 132
31
votes
1 answer

Migrate Global.asax to Startup.cs

For better test job with Microsoft.Owin.Testing.TestServer, I found that Global.asax is not loaded with Owin TestServer. So, I try to move my Global.asax configurations to Startup.cs as below, public partial class Startup { public void…
Youngjae
  • 21,562
  • 14
  • 100
  • 182
31
votes
4 answers

Web API 2, OWIN Authentication, SignOut doesn't logout

I'm doing some research for work with a view to using Bearer tokens as an authentication mechanism (i.e. AngularJS UI, authenticates via OWIN in a Web API [2] project). I have the login working fine, role information and all that is fine, but I…
toepoke.co.uk
  • 827
  • 1
  • 7
  • 18
30
votes
4 answers

Unable to get request header in asp net core web API

I am trying to create a custom filter in asp net core web api which is as below but unable to get header info. internal class BasicAuthFilterAttribute : ActionFilterAttribute { private StringValues xyz; public override void…
Jyotish Singh
  • 1,763
  • 1
  • 15
  • 25
30
votes
2 answers

RoutePrefix vs Route

I understand that RoutePrefix doesn't add a route to the routing table by itself. On your actions you need to have a Route attribute declared. I am having a hard time finding an authoritative blog/msdn page/ something that states why by defalut…
29
votes
5 answers

How to return JSON in an ApiController for a single method?

Currently, my ApiControllers are returning XML as a response, but for a single method, I want to return JSON. i.e. I can't make a global change to force responses as JSON. public class CarController : ApiController { …
29
votes
2 answers

Web API self host - bind on all network interfaces

How do you make a Web API self host bind on all network interfaces? I have the below code currently. Unfortunately, it binds only on localhost. So access to this server from other than localhost is failing. var baseAddress =…
govin
  • 5,857
  • 5
  • 35
  • 54