Questions tagged [asp.net-web-api]
ASP.NET Web API 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.
asp.net-web-api
38,067
questions
1219
votes
29
answers
985k
views
How do I get ASP.NET Web API to return JSON instead of XML using Chrome?
Using the newer ASP.NET Web API, in Chrome I am seeing XML - how can I change it to request JSON so I can view it in the browser? I do believe it is just part of the request headers, am I correct in ...
682
votes
18
answers
525k
views
Does IMDB provide an API? [closed]
I recently found a movie organizer application which fetches its data from the IMDB database.
Does IMDB provide an API for this, or any third party APIs available?
680
votes
4
answers
285k
views
Best practice to call ConfigureAwait for all server-side code
When you have server-side code (i.e. some ApiController) and your functions are asynchronous - so they return Task<SomeObject> - is it considered best practice that any time you await functions ...
532
votes
18
answers
453k
views
Pass an array of integers to ASP.NET Web API?
I have an ASP.NET Web API (version 4) REST service where I need to pass an array of integers.
Here is my action method:
public IEnumerable<Category> GetCategories(int[] categoryIds){
// code ...
504
votes
11
answers
226k
views
WCF vs ASP.NET Web API [closed]
I've spent a few months trying to grasp the concepts behind WCF and recently I've developed my first WCF service application.
I've struggled quite a bit to understand all the settings in the config ...
496
votes
6
answers
969k
views
Is there a <meta> tag to turn off caching in all browsers? [duplicate]
I read that when you don't have access to the web server's headers you can turn off the cache using:
<meta http-equiv="Cache-Control" content="no-store" />
But I also read that this doesn't ...
464
votes
15
answers
752k
views
Best practice to return errors in ASP.NET Web API
I have concerns on the way that we returns errors to client.
Do we return error immediately by throwing HttpResponseException when we get an error:
public void Post(Customer customer)
{
if (...
434
votes
11
answers
640k
views
POSTing JsonObject With HttpClient From Web API
I'm trying to POST a JsonObject using HttpClient from Web API. I'm not quite sure how to go about this and can't find much in the way of sample code.
Here's what I have so far:
var myObject = (...
407
votes
8
answers
313k
views
Difference between ApiController and Controller in ASP.NET MVC
I've been playing around with ASP.NET MVC 4 beta and I see two types of controllers now: ApiController and Controller.
I'm little confused at what situations I can choose a particular controller.
...
407
votes
6
answers
325k
views
How to secure an ASP.NET Web API [closed]
I want to build a RESTful web service using ASP.NET Web API that third-party developers will use to access my application's data.
I've read quite a lot about OAuth and it seems to be the standard, ...
373
votes
9
answers
241k
views
How to add Web API to an existing ASP.NET MVC 4 Web Application project?
I wish to add an ASP.NET Web API to an ASP.NET MVC 4 Web Application project, developed in Visual Studio 2012. Which steps must I perform to add a functioning Web API to the project? I'm aware that I ...
365
votes
16
answers
1.2m
views
Refused to display in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN' [duplicate]
I am developing a website that is supposed to be responsive so that people can access it from their phones. The site has got some secured parts that can be logged into using Google, Facebook, etc. (...
364
votes
9
answers
252k
views
Where is HttpContent.ReadAsAsync?
I see in tons of examples on the web using the new HttpClient object (as part of the new Web API) that there should be HttpContent.ReadAsAsync<T> method. However, MSDN doesn't mention this ...
363
votes
8
answers
259k
views
Why should I use IHttpActionResult instead of HttpResponseMessage?
I have been developing with WebApi and have moved on to WebApi2 where Microsoft has introduced a new IHttpActionResult Interface that seems to recommended to be used over returning a ...
357
votes
7
answers
434k
views
JWT authentication for ASP.NET Web API
I'm trying to support JWT bearer token (JSON Web Token) in my web API application and I'm getting lost.
I see support for .NET Core and for OWIN applications.
I'm currently hosting my application in ...
350
votes
9
answers
915k
views
How to pass json POST data to Web API method as an object?
ASP.NET MVC4 Web API application defines post method to save customer.
Customer is passed in json format in POST request body.
Customer parameter in post method contains null values for properties.
...
348
votes
8
answers
331k
views
Returning binary file from controller in ASP.NET Web API
I'm working on a web service using ASP.NET MVC's new WebAPI that will serve up binary files, mostly .cab and .exe files.
The following controller method seems to work, meaning that it returns a file, ...
336
votes
7
answers
324k
views
How do I set a cookie on HttpClient's HttpRequestMessage
I am trying to use the web api's HttpClient to do a post to an endpoint that requires login in the form of an HTTP cookie that identifies an account (this is only something that is #ifdef'ed out of ...
322
votes
15
answers
367k
views
HttpClient not supporting PostAsJsonAsync method C#
I am trying to call a web API from my web application. I am using .Net 4.5 and while writing the code I am getting the error HttpClient does not contain a definition PostAsJsonAsync method.
Below is ...
302
votes
13
answers
314k
views
Accessing Session Using ASP.NET Web API
I realize session and REST don't exactly go hand in hand but is it not possible to access session state using the new Web API? HttpContext.Current.Session is always null.
301
votes
5
answers
89k
views
ServiceStack vs ASP.Net Web API [closed]
I want to write a new REST style API and have looked at ServiceStack and quite like it. However, I have seen that Microsoft has released the ASP.Net Web API project as part of the new MVC 4 beta. Has ...
297
votes
12
answers
181k
views
Exposing localhost to the internet via tunneling (using ngrok): HTTP error 400: bad request; invalid hostname
From previous versions of the question, there is this: Browse website with ip address rather than localhost, which outlines pretty much what I've done so far...I've got the local IP working. Then I ...
274
votes
20
answers
459k
views
HTTP Error 500.19 and error code : 0x80070021
I have a simple webAPI build by Visual Studio 2013. It works well when I run it from VS13 but when I copy the project in local IIS it gives me the following error.
HTTP Error 500.19 - Internal Server ...
273
votes
18
answers
241k
views
Multiple actions were found that match the request in Web Api
I keep getting this error when I try to have 2 "Get" methods
Multiple actions were found that match the request: webapi
I been looking around at the other similar questions about this on stack ...
265
votes
31
answers
263k
views
Swagger UI Web Api documentation Present enums as strings?
Is there a way to display all enums as their string value in swagger instead of their int value?
I want to be able to submit POST actions and put enums according to their string value without having ...
263
votes
14
answers
471k
views
Returning http status code from Web Api controller
I'm trying to return a status code of 304 not modified for a GET method in a web api controller.
The only way I succeeded was something like this:
public class TryController : ApiController
{
...
262
votes
8
answers
156k
views
Microsoft Web API: How do you do a Server.MapPath?
Since Microsoft Web API isn't MVC, you cannot do something like this:
var a = Request.MapPath("~");
nor this
var b = Server.MapPath("~");
because these are under the System.Web namespace, not the ...
248
votes
23
answers
365k
views
Entity Framework Core: A second operation started on this context before a previous operation completed
I'm working on a ASP.Net Core 2.0 project using Entity Framework Core
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.1" />
<PackageReference ...
246
votes
6
answers
310k
views
Optional query string parameters in ASP.NET Web API
I need to implement the following WebAPI method:
/api/books?author=XXX&title=XXX&isbn=XXX&somethingelse=XXX&date=XXX
All of the query string parameters can be null. That is, the ...
244
votes
8
answers
452k
views
How to return a file (FileContentResult) in ASP.NET WebAPI
In a regular MVC controller, we can output pdf with a FileContentResult.
public FileContentResult Test(TestViewModel vm)
{
var stream = new MemoryStream();
//... add content to the stream.
...
230
votes
8
answers
346k
views
Put content in HttpResponseMessage object?
Several months ago, Microsoft decided to change up the HttpResponseMessage class. Before, you could simply pass a data type into the constructor, and then return the message with that data, but not ...
225
votes
43
answers
432k
views
Post parameter is always null
Since upgrading to RC for WebAPI I'm having some real odd issue when calling POST on my WebAPI.
I've even gone back to the basic version generated on new project. So:
public void Post(string value)
{
...
222
votes
17
answers
309k
views
Return content with IHttpActionResult for non-OK response
For returning from a Web API 2 controller, I can return content with the response if the response is OK (status 200) like this:
public IHttpActionResult Get()
{
string myResult = ...
return Ok(...
220
votes
5
answers
564k
views
.NET HttpClient. How to POST string value?
How can I create using C# and HttpClient the following POST request:
I need such a request for my WEB API service:
[ActionName("exist")]
[HttpPost]
public bool CheckIfUserExist([FromBody] string ...
219
votes
5
answers
597k
views
How to write a JSON file in C#?
I need to write the following data into a text file using JSON format in C#. The brackets are important for it to be valid JSON format.
[
{
"Id": 1,
"SSN": 123,
"Message": "whatever"
...
218
votes
17
answers
398k
views
How to read request body in an asp.net core webapi controller?
I'm trying to read the request body in the OnActionExecuting method, but I always get null for the body.
var request = context.HttpContext.Request;
var stream = new StreamReader(request.Body);
var ...
215
votes
13
answers
215k
views
How to add/update child entities when updating a parent entity in EF
The two entities are one-to-many relationship (built by code first fluent api).
public class Parent
{
public Parent()
{
this.Children = new List<Child>();
}
public int ...
212
votes
2
answers
251k
views
Adding Http Headers to HttpClient
I need to add http headers to the HttpClient before I send a request to a web service. How do I do that for an individual request (as opposed to on the HttpClient to all future requests)? I'm not sure ...
210
votes
11
answers
319k
views
How to get HttpClient to pass credentials along with the request?
I have a web application (hosted in IIS) that talks to a Windows service. The Windows service is using the ASP.Net MVC Web API (self-hosted), and so can be communicated with over http using JSON. The ...
209
votes
8
answers
273k
views
Specifying a custom DateTime format when serializing with Json.Net
I am developing an API to expose some data using ASP.NET Web API.
In one of the API, the client wants us to expose the date in yyyy-MM-dd format. I don't want to change the global settings (e.g. ...
207
votes
4
answers
241k
views
Why do we have to specify FromBody and FromUri?
Why are the FromBody and FromUri attributes needed in ASP.NET Web API`?
What are the differences between using the attributes and not using them?
205
votes
28
answers
428k
views
Could not load file or assembly 'System.Web.Http 4.0.0 after update from 2012 to 2013
I did the upgrade according to.
http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2
I get the error. Does any one else got this ...
203
votes
34
answers
350k
views
The CodeDom provider type "Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider" could not be located
It's a WebApi project using VS2015.
Step to reproduce:
Create an empty WebApi project
Change Build output path from "bin\" to "bin\Debug\"
Run
Everything is working perfectly until I changed Build ...
200
votes
20
answers
326k
views
Single controller with multiple GET methods in ASP.NET Web API
In Web API I had a class of similar structure:
public class SomeController : ApiController
{
[WebGet(UriTemplate = "{itemSource}/Items")]
public SomeValue GetItems(CustomParam parameter) { ......
197
votes
14
answers
419k
views
Make Https call using HttpClient
I have been using HttpClient for making WebApi calls using C#. Seems neat & fast way compared to WebClient. However I am stuck up while making Https calls.
How can I make below code to make Https ...
194
votes
2
answers
123k
views
How do you implement an async action delegate method?
A little background information.
I am learning the Web API stack and I am trying to encapsulate all data in the form of a "Result" object with parameters such as Success and ErrorCodes.
...
193
votes
7
answers
103k
views
What is the overhead of creating a new HttpClient per call in a WebAPI client?
What should be the HttpClient lifetime of a WebAPI client?
Is it better to have one instance of the HttpClient for multiple calls?
What's the overhead of creating and disposing a HttpClient per ...
191
votes
13
answers
199k
views
prevent property from being serialized in web API
I'm using an MVC 4 web API and asp.net web forms 4.0 to build a rest API. It's working great:
[HttpGet]
public HttpResponseMessage Me(string hash)
{
HttpResponseMessage httpResponseMessage;
...
190
votes
9
answers
124k
views
WebApi's {"message":"an error has occurred"} on IIS7, not in IIS Express
I'm working with ASP.NET MVC 4 WebApi and am having a lot of fun with it running it on my local computer on IIS Express. I've configured IIS Express to serve remote machines too, and so other's in my ...
189
votes
15
answers
106k
views
Unauthorised webapi call returning login page rather than 401
How do I configure my mvc/webapi project so that a webapi method called from a razor view doesn't return the loginpage when its unauthorised?
Its a MVC5 application which also has WebApi controllers ...