相关疑难解决方法(0)

为什么我们必须指定FromBody和FromUri?

为什么ASP.NET Web API需要FromBodyFromUri属性?

使用属性和不使用它们之间有什么区别?

asp.net-web-api

140
推荐指数
4
解决办法
12万
查看次数

C#HttpClient 4.5 multipart/form-data上传

有没有人知道如何使用HttpClient.Net 4.5 multipart/form-data上传?

我在互联网上找不到任何例子.

c# upload multipartform-data .net-4.5 dotnet-httpclient

128
推荐指数
6
解决办法
16万
查看次数

POST字符串到ASP.NET Web Api应用程序 - 返回null

我试图从客户端传输一个字符串到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)

c# asp.net asp.net-mvc post asp.net-web-api

40
推荐指数
2
解决办法
11万
查看次数

ASP.Net Web API模型绑定不像MVC 3那样工作

我的印象是,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

25
推荐指数
1
解决办法
1万
查看次数