Tag: asp.net web api

Web API复杂参数属性均为null

我有一个Web API服务调用,用于更新用户的首选项。 不幸的是,当我从jQuery ajax调用调用此POST方法时,请求参数对象的属性始终为null(或默认值),而不是传入的内容。如果我使用REST客户端调用相同的方法(我使用Postman) ,它工作得很漂亮。 我无法弄清楚我做错了什么,但我希望有人见过这个。 这很简单…… 这是我的请求对象: public class PreferenceRequest { [Required] public int UserId; public bool usePopups; public bool useTheme; public int recentCount; public string[] detailsSections; } 这是UserController类中的控制器方法: public HttpResponseMessage Post([FromBody]PreferenceRequest request) { if (request.systemsUserId > 0) { TheRepository.UpdateUserPreferences(request.UserId, request.usePopups, request.useTheme, request.recentCount, request.detailsSections); return Request.CreateResponse(HttpStatusCode.OK, “Preferences Updated”); } else { return Request.CreateErrorResponse(HttpStatusCode.NotAcceptable, “You […]

jQuery将null而不是JSON发布到ASP.NET Web API

我似乎无法让这个工作……我在客户端上有一些像这样的jQuery: $.ajax({ type: “POST”, url: “api/report/reportexists/”, data: JSON.stringify({ “report”:reportpath }), success: function(exists) { if (exists) { fileExists = true; } else { fileExists = false; } } }); 在我的Web.API控制器中,我有一个这样的方法: [HttpPost] public bool ReportExists( [FromBody]string report ) { bool exists = File.Exists(report); return exists; } 我只是检查一个文件是否存在于服务器上,并返回一个bool是否存在。 我发送的报告字符串是UNC路径,因此reportpath看起来像’\\ some \ path \’。 我可以解决脚本问题,并在ReportExists方法中点击断点,但报表变量始终为null。 我究竟做错了什么? 我也看到了使用.post和postJSON发布的方法。 也许我应该使用其中之一? 如果是这样,我的格式是什么? […]

文件上传Jquery WebApi

我使用以下代码将文件上传到服务器,但文件未上传。 HTML: Upload 使用Javascript: // Hook into the form’s submit event. $(‘#upload’).submit(function () { // To keep things simple in this example, we’ll // use the FormData XMLHttpRequest Level 2 object (which // requires modern browsers eg IE10+, Firefox 4+, Chrome 7+, Opera 12+ etc). var formData = new FormData(); // We’ll grab our file […]

将JSON对象发送到Web API

我试图找出如何将表单中的一些信息发送到Web API操作。 这是我试图使用的jQuery / AJAX: var source = { ‘ID’: 0, ‘ProductID’: $(‘#ID’).val(), ‘PartNumber’: $(‘#part-number’).val(), ‘VendorID’: $(‘#Vendors’).val() } $.ajax({ type: “POST”, dataType: “json”, url: “/api/PartSourceAPI/”, data: JSON.stringify({ model: source }), success: function (data) { alert(‘success’); }, error: function (error) { jsonValue = jQuery.parseJSON(error.responseText); jError(‘An error has occurred while saving the new part source: ‘ + […]