如何在MVC webApi控制器操作中读取PUT请求中的内容.
[HttpPut]
public HttpResponseMessage Put(int accountId, Contact contact)
{
var httpContent = Request.Content;
var asyncContent = httpContent.ReadAsStringAsync().Result;
...
Run Code Online (Sandbox Code Playgroud)
我在这里得到空字符串:(
我需要做的是:弄清楚在初始请求中修改/发送了什么属性(意味着如果Contact对象有10个属性,并且我只想更新其中2个属性,我只发送和对象只有两个属性,这样的事情:
{
"FirstName": null,
"LastName": null,
"id": 21
}
Run Code Online (Sandbox Code Playgroud)
预期的最终结果是
List<string> modified_properties = {"FirstName", "LastName"}
Run Code Online (Sandbox Code Playgroud) 我正在尝试从新的Asp.Net Web Api中的请求中提取一些数据.我有这样的处理程序设置:
public class MyTestHandler : DelegatingHandler
{
protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
if (request.Content.IsFormData())
{
request.Content.ReadAsStreamAsync().ContinueWith(x => {
var result = "";
using (var sr = new StreamReader(x.Result))
{
result = sr.ReadToEnd();
}
Console.Write(result);
});
}
return base.SendAsync(request, cancellationToken);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的http请求:
POST http://127.0.0.1/test HTTP/1.1
Connection: Keep-Alive
Content-Length: 29
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue
Host: 127.0.0.1
my_property=my_value
Run Code Online (Sandbox Code Playgroud)
问题是无论我如何尝试从中读取信息request.Content总是空的.我试过了
request.Content.ReadAsStreamAsync
request.Content.ReadAsFormDataAsync
request.Content.ReadAs<FormDataCollection>
Run Code Online (Sandbox Code Playgroud)
以及
[HttpGet,HttpPost]
public string Index([FromBody]string my_property)
{
//my_property == null
return "Test";
} …Run Code Online (Sandbox Code Playgroud) 我是WebApi的新手,现在我有两个像这样的httpPost方法
[HttpPost]
public List<YellowPages.Person> AddPersonDetails(YellowPages.Person person)
{
Repository.Repository.personsList.Add(person);
return Repository.Repository.personsList;
}
Run Code Online (Sandbox Code Playgroud)
第二种方法是
[HttpPost]
public List<YellowPages.City> getRelevantCity(string stateID)
{
return new Repository.YellowPages.City().getCity()
.Where(x => x.StateID ==stateID).ToList();
}
Run Code Online (Sandbox Code Playgroud)
每当我调用getRelevantCity方法时,都会调用AddPersonDetails方法,我相信这与REST架构有关.现在我的问题是如何处理这种情况.我在WebApiConfig.cs文件中有什么可以做的并添加约束吗?如果是,如何处理我正在使用的模型类型的约束.谢谢.
如我所知,我已经改变了我删除属性的方法,如下所示
public List<YellowPages.Person> AddPersonDetails(YellowPages.Person person)
{
Repository.Repository.personsList.Add(person);
return Repository.Repository.personsList;
}
public List<YellowPages.City> getRelevantCity(string stateID)
{
return new Repository.YellowPages.City().getCity().Where(x => x.StateID == stateID).ToList();
}
Run Code Online (Sandbox Code Playgroud)
我的ajax电话是这样的
$('#StateID').change(function () {
$.ajax({
type: 'POST',
url: '/api/shoppingCart/getRelevantCity',
ContentType: 'application/json',
data: { 'stateID': $('#StateID').val() },
dataType: 'json',
success: function (returnData) {
var grid = '';
$.each(returnData, function (i, …Run Code Online (Sandbox Code Playgroud)