asp.net-web-api

Ajax request with custom header sent to Web API server with CORS enabled

我的梦境 提交于 2021-02-07 19:41:31
问题 I'm trying to get a Web API webservice setup correctly to use CORS but haven't been having much luck with it lately. I have an html page from a mobile app using an ajax request to fetch some data: Webservices.GetUserLevel = function() { var userLevel = null; $.ajax({ url: _CONNECTION_STRING + "getuserlevel/" + _USER_PIN, contentType:'application/json; charset=utf-8', type: 'GET', async: false, cache: false, crossDomain: true, data: { BlackberryPin: _USER_PIN }, beforeSend: function(xhr) { xhr

C# WEB API CORS does not work

﹥>﹥吖頭↗ 提交于 2021-02-07 14:25:41
问题 Fighting with CORS. I have a site that is making a simple XmlHttpRequest to a WEB API I built in C#. var xhr = new XMLHttpRequest(); xhr.open("GET","https://server/controller/method", true); xhr.send(); In my web.config I have done the following: <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> </customHeaders> </httpProtocol> I have also tried installing the Nuget package and doing the following in my WebApiConfig.cs var cors = new EnableCorsAttribute(

JSON Array of Objects to Model in WebAPI using FromBody

放肆的年华 提交于 2021-02-07 14:14:45
问题 I am creating a Web Api method that should accept a list of objects via XML or JSON and add them to a database. Here is a very basic version of what I currently have: [HttpPost] public HttpResponseMessage Put([FromBody]ProductAdd productAdd) { //do stuff with productadd object return Request.CreateResponse(HttpStatusCode.OK); } The model structure of the list of objects it accepts is as follows: public class ProductAdd { public List<ProductInformation> Products { get; set; } } public class

Calling Web Api POST always receives null value from jQuery

安稳与你 提交于 2021-02-07 13:26:45
问题 I'm using ASP.NET Web API, and I'm having a wonderful time. At least with GETs. For some reason when I try to send data via jQuery $.post() or $.ajax() , the values received by my ApiController are always null. What's more strange is that sending data to it with Fiddler does just fine. I'm sure it's a problem with how I'm constructing the object in javascript, but I can't seem to find it. Here is the code: // C# [HttpPost] public HttpResponseMessage BeginTrack([FromBody]string requestContext)

Calling Web Api POST always receives null value from jQuery

大兔子大兔子 提交于 2021-02-07 13:24:33
问题 I'm using ASP.NET Web API, and I'm having a wonderful time. At least with GETs. For some reason when I try to send data via jQuery $.post() or $.ajax() , the values received by my ApiController are always null. What's more strange is that sending data to it with Fiddler does just fine. I'm sure it's a problem with how I'm constructing the object in javascript, but I can't seem to find it. Here is the code: // C# [HttpPost] public HttpResponseMessage BeginTrack([FromBody]string requestContext)

400 bad request when access ASP.net web api via computer name on IISExpress

醉酒当歌 提交于 2021-02-07 13:21:44
问题 I'm getting a 400 bad request error message when accessing an ASP.net web api via a url that uses the computer name. When I use localhost it works fine. I'm using the iisExpress for visual studio 2012. Any help is appreciated! Thanks in advance! 回答1: I found the answer to this from another post on here. I had to update my IISExpress' applicationhost.config file and add a binding with my computer name on it. The link to the other post is here: Configure IIS Express for external access to

400 bad request when access ASP.net web api via computer name on IISExpress

情到浓时终转凉″ 提交于 2021-02-07 13:18:23
问题 I'm getting a 400 bad request error message when accessing an ASP.net web api via a url that uses the computer name. When I use localhost it works fine. I'm using the iisExpress for visual studio 2012. Any help is appreciated! Thanks in advance! 回答1: I found the answer to this from another post on here. I had to update my IISExpress' applicationhost.config file and add a binding with my computer name on it. The link to the other post is here: Configure IIS Express for external access to

Why use OWIN TestServer?

点点圈 提交于 2021-02-07 12:53:52
问题 This article shows how to host an entire web API stack in memory for testing using OWIN: http://www.davidwhitney.co.uk/Blog/2015/01/07/testing-an-asp-net-webapi-app-in-memory/ Whereas this article shows using the OWIN TestServer to unit test controllers: https://blog.jcorioland.io/archives/2014/04/01/using-owin-to-test-your-web-api-controllers.html The difference I see is between the use of TestServer.Create and WebApp.Start<Startup> What is the key difference and why would you choose one

How to Change ConnectionStrings at Runtime for a Web API

試著忘記壹切 提交于 2021-02-07 12:27:34
问题 I hope this is a simple question: How can you change 2 connection strings at runtime in the Global.asax under Application_Start() Web.config <connectionStrings> <add connectionString="DB1" value=""/> <add connectionString="DB2" value=""/> </connectionStrings> Global.asax protected void Application_Start() { AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable

HttpClient Instancing Per Service-Endpoint

谁说胖子不能爱 提交于 2021-02-07 11:19:20
问题 When instancing an HttpClient, the one common piece of advice is: Use a singleton, do not dispose after each use. However, based on this link I see commentary which I think implies another rule: The HttpClient class instance acts as a session to send HTTP requests. An HttpClient instance is a collection of settings applied to all requests executed by that instance. In addition, every HttpClient instance uses its own connection pool, isolating its requests from requests executed by other