为什么ASP.NET Web API需要FromBody和FromUri属性?
使用属性和不使用它们之间有什么区别?
有没有人知道如何使用HttpClient.Net 4.5 multipart/form-data上传?
我在互联网上找不到任何例子.
我试图从客户端传输一个字符串到ASP.NET MVC4应用程序.
但我无法接收字符串,无论是null还是找不到post方法(404错误)
客户端代码传输字符串(控制台应用程序):
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:49032/api/test");
request.Credentials = new NetworkCredential("user", "pw");
request.Method = "POST";
string postData = "Short test...";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
ASP.NET Web Api控制器:
public class TestController : ApiController
{
[Authorize]
public String Post(byte[] value)
{
return value.Length.ToString();
}
} …Run Code Online (Sandbox Code Playgroud) 我的印象是,ASP.Net Web API中的模型绑定应该支持具有MVC支持的相同最低功能级别的绑定.
拿以下控制器:
public class WordsController : ApiController
{
private string[] _words = new [] { "apple", "ball", "cat", "dog" };
public IEnumerable<string> Get(SearchModel searchSearchModel)
{
return _words
.Where(w => w.Contains(searchSearchModel.Search))
.Take(searchSearchModel.Max);
}
}
public class SearchModel
{
public string Search { get; set; }
public int Max { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我要求它:
http://localhost:62855/api/words?search=a&max=2
Run Code Online (Sandbox Code Playgroud)
不幸的是,模型没有像在MVC中那样绑定.为什么这不像我期望的那样具有约束力?我的应用程序中会有很多不同的模型类型.如果绑定工作就好了,就像在MVC中一样.
model-binding custom-model-binder asp.net-web-api jquery-datatables-editor