C# 为什么字符串是';axios中的s参数为空?
我在axios中向api发送一个参数,然后运行linq以获得结果 不幸的是,当涉及到api中的函数时,我得到了null值 我错在哪里 守则:C# 为什么字符串是';axios中的s参数为空?,c#,reactjs,asp.net-core,axios,C#,Reactjs,Asp.net Core,Axios,我在axios中向api发送一个参数,然后运行linq以获得结果 不幸的是,当涉及到api中的函数时,我得到了null值 我错在哪里 守则: react: const code= "sdasdasdadad" axios.post('api/UserDetails/', code).then((response) => setUserDetails(response.data)); 如果您真的希望您的API能够很好地处理纯文本输入,您可以尝试实现自定
react:
const code= "sdasdasdadad"
axios.post('api/UserDetails/', code).then((response) => setUserDetails(response.data));
如果您真的希望您的API能够很好地处理纯文本输入,您可以尝试实现自定义纯文本输入格式化程序,如下所示
public class TextPlainInputFormatter : TextInputFormatter
{
public TextPlainInputFormatter()
{
SupportedMediaTypes.Add("text/plain");
SupportedEncodings.Add(UTF8EncodingWithoutBOM);
SupportedEncodings.Add(UTF16EncodingLittleEndian);
}
protected override bool CanReadType(Type type)
{
return type == typeof(string);
}
public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
{
string data = null;
using (var streamReader = new StreamReader(context.HttpContext.Request.Body))
{
data = await streamReader.ReadToEndAsync();
}
return InputFormatterResult.Success(data);
}
}
API行动
使用axios发出请求
测试结果
axios.post的第二个参数必须是JS对象如果我只想发送一个字符串怎么办?怎么写?看一看
public class TextPlainInputFormatter : TextInputFormatter
{
public TextPlainInputFormatter()
{
SupportedMediaTypes.Add("text/plain");
SupportedEncodings.Add(UTF8EncodingWithoutBOM);
SupportedEncodings.Add(UTF16EncodingLittleEndian);
}
protected override bool CanReadType(Type type)
{
return type == typeof(string);
}
public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
{
string data = null;
using (var streamReader = new StreamReader(context.HttpContext.Request.Body))
{
data = await streamReader.ReadToEndAsync();
}
return InputFormatterResult.Success(data);
}
}
services.AddControllers(opt => opt.InputFormatters.Insert(0, new TextPlainInputFormatter()));
[Microsoft.AspNetCore.Mvc.Route("UserDetails")]
[Microsoft.AspNetCore.Mvc.HttpPost]
public Users UserDetails([FromBody]string code)
{
//...
const code = "sdasdasdadad";
const headers = {
'Content-Type': 'text/plain'
};
axios.post('api/UserDetails', code, { headers: headers }).then((response) => setUserDetails(response.data));