Tag: frombodyattribute

C#Web API POST参数FromBody始终为null

我已经在网上搜了好几个小时,并尝试了StackOverflow上描述的许多不同的解决方案。 我之前已经问过类似的问题,但没有一个答案或评论对我有用。 问题:我有一个.NET Web API,它有一个带有一些参数的Post方法。 其中一个参数是一个复杂的对象,应该从正文中读取(即JSON)。 但是,此对象始终为null 。 这是我的代码: // POST api/worksheets/post_event/true/false [Route(“post_event/{newWorksheet}/{eindEvent}”)] [HttpPost] public Event Post(bool newWorksheet, bool eindEvent, [FromBody] Event eventData) { return eventData; } 要明确:eventData是始终为null的对象。 布尔值被正确读取。 完整的请求正文是: POST http://localhost:5000/api/worksheets/post_event/true/false Content-Type: application/json {“Persnr”:1011875, “WorksheetId”:null, “Projectnr”:81445, “Uursoort”:8678, “Tijd”:{“09-08-2016 9:25”}} 作为参考,这是Event-class: public class Event { public long Persnr { get; set; } public int WorksheetId […]

如何将大量数据传递到我的Web API应用程序?

我在Web API控制器中有这个代码: [Route(“{unit}/{begindate}/{enddate}”)] [HttpPost] public void Post(string unit, string begindate, string enddate, [FromBody] string stringifiedjsondata) { List _produceUsageList = JsonConvert.DeserializeObject<List>(stringifiedjsondata); string _unit = unit; string _begindate = String.Format(“{0}01”, HyphenizeYYYYMM(begindate)); string _enddate = GetEndOfMonthFor(enddate); string appDataFolder = HttpContext.Current.Server.MapPath(“~/App_Data/”); string htmlStr = ConvertProduceUsageListToHtml(_produceUsageList); string htmlFilename = string.Format(“produceUsage_{0}_{1}_{2}.html”, _unit, _begindate, _enddate); string fullPath = Path.Combine(appDataFolder, htmlFilename); File.WriteAllText(fullPath, htmlStr); […]

FromBody值为null

这是Asp.Net Webform应用程序 这是我的Apicontroller中的POST方法 public void Post([FromBody]string value) { } 我是小提琴后期流程。 我做了这样的实验。 但事实并非如此。 问题是什么。 你能帮我吗? 我试过了,我失败了。 public void Post(MyViewModel model) { string aa = model.Value; } public class MyViewModel { public string Value { get; set; } } 在提琴手: Request Body: Value=hakan

如何通过FromBody将DataTable传递给Web API POST方法(C#)

我从Winforms客户端成功调用Web API应用程序中的POST方法,该客户端传递存储过程的一些参数。 但是,如果可能的话,我希望通过FromBodyfunction将存储过程的结果(我必须首先在客户端上运行)传递给POST方法。 这是通过网络发送的大量数据,但我现在正在这样做的方式我必须运行SP两次 – 首先在客户端Winforms应用程序上,然后在Web API服务器应用程序上,并同时调用此SP似乎有时会引起一些问题。 所以,如果可行,我想通过“FromBody”发送DataTable,或者,如果可取的话,发送数据的XML化或jsonized版本(然后在另一端解压缩,我将其转换为html进行检索)当调用相应的GET方法时。 有人有任何代码可以显示吗? 我可以在这里看到我刚刚通过params的现有代码。 UPDATE 好的,基于Amit Kumar Ghosh的回答,我将我的代码更改为: WebApiConfig.cs 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.SuppressDefaultHostAuthentication(); config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: “DefaultApi”, routeTemplate: “api/{controller}/{id}”, […]

同时阅读FromUri和FromBody

我在web api中有一个新方法 [HttpPost] public ApiResponse PushMessage( [FromUri] string x, [FromUri] string y, [FromBody] Request Request) 请求类就像 public class Request { public string Message { get; set; } public bool TestingMode { get; set; } } 我正在使用PostBody对localhost / Pusher / PushMessage进行查询?x = foo&y = bar: { Message: “foobar” , TestingMode:true } 我错过了什么吗?