自从升级到RC for WebAPI后,我在WebAPI上调用POST时遇到了一些奇怪的问题.我甚至回到了新项目生成的基本版本.所以:
public void Post(string value)
{
}
Run Code Online (Sandbox Code Playgroud)
并从Fiddler打来电话:
Header:
User-Agent: Fiddler
Host: localhost:60725
Content-Type: application/json
Content-Length: 29
Body:
{
"value": "test"
}
Run Code Online (Sandbox Code Playgroud)
当我调试时,字符串"value"永远不会被分配给.它总是为NULL.谁有这个问题?
(我第一次看到更复杂类型的问题)
问题不仅仅是绑定到ASP.NET MVC 4,在安装RC后,新的ASP.NET MVC 3项目也会出现同样的问题
类似这种关于ASP.NET之前老版本的问题,我希望得到一个HTTP POST请求体被绑定到一个字符串.value当ASP.NET调用我的控制器方法时,似乎该方法绑定,但是为null:
namespace Demo.Controllers
{
[Route("[controller]")]
public class WebApiDemoController : Controller
{
...
// POST api/values
[HttpPost]
public System.Net.Http.HttpResponseMessage Post([FromBody]string value)
{
// expected: value = json string, actual: json = null.
}
Run Code Online (Sandbox Code Playgroud)
我还需要从溪流中抓住身体吗?或者这应该工作吗?在测试上述方法时,我使用了以下http标头:
Accept: Application/json
Content-Type: Application/json;charset=UTF-8
Run Code Online (Sandbox Code Playgroud)
我在身体中传递以下内容: { "a": 1 }
我不想绑定到名为a的字符串变量.我想绑定任何我得到的JSON,然后我想在我的方法中使用JSON内容,任何任意内容.
如果我理解了文档,该[FromBody]属性应该已经完成了我想要的,但我猜测ASP.NET核心MVC绑定机制不会将json绑定到"字符串值",但也许我可以做其他事情让我获得同等程度的灵活性.
类似的问题在这里给了我一个想法,也许我应该写[FromBody] dynamic data而不是使用[FromBody] string value.
更新:在执行此操作之前应该考虑这种技巧,因为如果您希望.net核心框架为您处理JSON和XML编码,那么您刚刚杀死了该功能.某些类型的REST服务器可以并且通常具有支持XML和JSON内容类型的要求,至少我遇到过具有标准文档的内容类型.
我试图从客户端传输一个字符串到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中使用api控制器,我需要通过[FromBody]类型将值传递给post方法.
[HttpPost]
public HttpResponseMessage Post( [FromBody]string name)
{
....
}
Run Code Online (Sandbox Code Playgroud)
我使用Postman插件但是当发送到post方法时,name的值总是为null ..请按照下图:

为什么会这样?!
我有一个现有的Web API 2服务,需要修改其中一个方法以将自定义对象作为另一个参数,目前该方法有一个参数,它是来自URL的简单字符串.将自定义对象添加为参数后,从.NET Windows应用程序调用服务时,我现在收到415不支持的媒体类型错误.有趣的是,我可以使用javascript和jquery ajax方法成功调用此方法.
Web API 2服务方法如下所示:
<HttpPost>
<HttpGet>
<Route("{view}")>
Public Function GetResultsWithView(view As String, pPaging As Paging) As HttpResponseMessage
Dim resp As New HttpResponseMessage
Dim lstrFetchXml As String = String.Empty
Dim lstrResults As String = String.Empty
Try
'... do some work here to generate xml string for the response
'// write xml results to response
resp.Content = New StringContent(lstrResults)
resp.Content.Headers.ContentType.MediaType = "text/xml"
resp.Headers.Add("Status-Message", "Query executed successfully")
resp.StatusCode = HttpStatusCode.OK
Catch ex As Exception
resp.StatusCode = HttpStatusCode.InternalServerError
resp.Headers.Add("Status-Message", String.Format("Error …Run Code Online (Sandbox Code Playgroud) vb.net json asp.net-web-api http-status-code-415 asp.net-web-api2
我在 ASP.NET Core 3.1 中实现了一个端点来检索大型 JSON 对象,当此 JSON 开始相对较大时,我遇到了问题。我开始在 5~600KB 左右出现问题。
对于正文低于 600~500KB 的请求,一切正常。
端点定义如下:
[DisableRequestSizeLimit]
[RequestFormLimits(ValueCountLimit = int.MaxValue)]
[HttpPost]
[Route("MyTestEndpoint")]
public void PostTest([FromBody]object objVal)
{
// objVal is null when the post is larger than ~5~600KB
string body = objVal.toString;
....
}
Run Code Online (Sandbox Code Playgroud)
网络配置:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.web>
<httpRuntime maxRequestLength="1048576" />
</system.web>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\ApiSLPCalendar.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
<security>
<requestFiltering>
<!-- This will handle requests …Run Code Online (Sandbox Code Playgroud) 我想将对象数据发送到我的Web API。api接受一个class参数,其属性是int和string的类型。
这是我的课:
public class deneme
{
public int ID { get; set; }
public int sayi { get; set; }
public int reqem { get; set; }
public string yazi { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的json对象:
{
"id":0,
"sayi":"9",
"reqem":8,
"yazi":"sss"
Run Code Online (Sandbox Code Playgroud)
}
我希望api将属性“ sayi”读取为整数。但是由于无法显示,因此出现错误:无法将JSON值转换为System.Int32。路径:$。sayi
我该如何解决这个问题?
我正在使用asp.net核心Web API。下面是我的简单post函数,它具有一个字符串参数。问题是当我使用[FromBody]时,字符串保持为空。我正在使用PostMan来测试我的服务。我希望原始数据从客户端传递到我的控制器。在Postman中,我选择主体类型RAW,并设置标题Content-Type文本/纯文本。Raw Body包含“ Hello World”字符串。
[HttpPost]
[Route("hosted-services/tokenize-card")]
public IActionResult Test([FromRoute]decimal businessKey,[FromBody] string body)
{
var data = businessKey;
return new JsonResult("Hello World");
}
Run Code Online (Sandbox Code Playgroud)