asp.net-apicontroller

Passing JSON Array from Javascript to Web API Controller method

[亡魂溺海] 提交于 2019-12-30 08:13:03
问题 I was not able to get the JSON array parameters in web api controller method (SaveDetails). Here are my code. JavaScript Code: $.ajax( { url : "api/Test/SaveDetails", type : "POST", data : { "employees": [ { "firstName": "John", "lastName": "Doe" }, { "firstName": "Anna", "lastName": "Smith" }, { "firstName": "Peter", "lastName": "Jones" } ] }, success: function (data) {alert("success");}, error: function () {alert("Error");} }) Controller method [HttpPost] public DataSet SaveDetails(Models

MVC Rest API - Unable to get Collection in json parameter

若如初见. 提交于 2019-12-25 08:23:22
问题 I'm trying to send this json to my controller { "Name":"Ford Focus", "CarProperties":[ { "Name":"Airbag" } ] } As you see there is a Collection named as CarProperties. I want to handle this collection in my Controller function. But getting null. Controller public class TestController : ApiController { public CarViewModel PostCar(CarViewModel car) { //Here is the error // Output is : {"Name":"Ford Focus"} so, car.CarProperties has come as null return car; } } View Models public class

How to call an ApiController from another .net project?

﹥>﹥吖頭↗ 提交于 2019-12-25 01:49:21
问题 I have an Asp.net Mvc3 project and I have made a ApiController in it. Now I want to call the api in a windows application project. And I dont know how to config Global.asax or web.config file to approach this. And I dont know what would be the service reference url(If the ApiController name is Service and the method in it is UpdatePrice)? 回答1: You have to make an HTTP request from the application to the WEBAPI action URL you want to access using HttpClient . WEBAPI is a REST based HTTP

API controller passed invalid JSON receives null

一曲冷凌霜 提交于 2019-12-14 03:24:19
问题 I'm using ASP.NET's ApiController class to create a Web API. I find that if I pass invalid JSON, instead of the caller getting a 500, the input parameter is null. As in, if I pass { "InputProperty:" "Some Value" } which is clearly not valid, to this method: [HttpPost] public Dto.OperationOutput Operation(Dto.OperationInput p_input) { return this.BusinessLogic.Operation(p_input); } I get that p_input is null . I'd rather send something back telling the user they didn't POST valid JSON. In my

How do I read in a file from disc during runtime in my WebAPI .NET Core [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-13 03:54:30
问题 This question already has answers here : How do I read a local file in my ASP.NET Core project (2 answers) Closed 11 months ago . In my solution I have a file called beep.png directly in the root, right next to Startup.cs file. I changed its properties to always copy . I activated UseFileServer and opted in to browse the directory structure to be sure. However, when I run the code Image.FromFile("beep.png"); , I only get the error that the file isn't found. System.IO.FileNotFoundException

Web API Routing - Only actions with a URI parameter work

旧城冷巷雨未停 提交于 2019-12-13 00:09:31
问题 I had a web API project that was working fine. I merged it with an MVC project, and now only the actions with a URI parameter work. All other actions end up with a 404 Not Found where even the controller is not found. Here's what I have in WebApiConfig (standard stuff): public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API configuration and services // Configure Web API to use only bearer token authentication. config

How to deserialize asp.net datetime as JSON datetime with client's timezone

ぃ、小莉子 提交于 2019-12-12 06:16:08
问题 I'm struggling with datetimes a bit. I'm using asp.net mvc api controllers, a microsoft sql server and AngularJS. On some button click I'm sending a JSON formatted date to an api-controller. When I post 2015-11-31 00:00 and I look in Fiddler to see what's really posted, I see that the date is formatted as such: 2015-11-30T23:00:00.000Z. (2015-11-31 - 1 hour UTC+01:00 Amsterdam, Berlin, Ber....) This is perfect because there might be a difference between the timezone the sql server might be in

WebAPI 2 not deserializing List<string> property of FromBody object in POST request

有些话、适合烂在心里 提交于 2019-12-11 17:53:23
问题 In one of my WebAPI 2 applications, I'm having trouble deserializing a List<string> property of a FromBody object. (The list stays empty, while the other properties are deserialized correctly.) Whatever I do, the property only seems to deserialize correctly if I change the property to a string[] . Unfortunately for me, the property needs to be of type List<string> . According to another question I found, I should be able to deserialize to a List<T> , as long as T is not an Interface . Is

MVC 4 return JSON as ActionResult

早过忘川 提交于 2019-12-11 00:42:55
问题 i'm trying to get my apicontroller to work. But somehow i cannot return Json() . Here's the error message from the compiler: Error CS0029 Cannot implicitly convert type 'System.Web.Http.Results.JsonResult<>' to 'System.Web.Mvc.JsonResult' Opten.Polyglott.Web D:\Development\git\Opten.Polyglott\src\Opten.Polyglott.Web\Controllers\NewsletterApiController.cs I cannot explain why it cannot convert the Json() to the ActionResult even the Json() inherits ActionResult . Here's my controller: using

ASP NET attribute routing array

我与影子孤独终老i 提交于 2019-12-11 00:35:26
问题 So I am using attribute routing for my controller like this: [Route("last/{id:int}")] public IHttpActionResult GetUpdateLast([FromUri] int id) { return Ok(); } Now I want to convert it to accept an array of integers, in the controller parameter I just switch to [FromUri] int id[] with no problems, however, how do I switch the route attribute [Route("last/{id:int}")] to make it accept an array of integers? 回答1: Your were almoust there, there is no way of doing what you want using the route, so