asp.net core API中post提交,得到的结果为什么是这样?
namespace WebApplication1.Controllers
{
[Route("ActionApi/[controller]")]
[ApiController]
public class UserInfoController : ControllerBase
{
//检查用户名是否已注册
private ApiTools tool = new ApiTools();
[Route("CheckUserName")]
[HttpPost("{username}")]
//[HttpPost]
public HttpResponseMessage CheckUserName(string _userName)
{
int num = UserInfoGetCount(_userName);//查询是否存在该用户
if (num > 0)
{
return tool.MsgFormat(ResponseCode.操作失败, "不可注册/用户已注册", "1 " + _userName);
}
else
{
return tool.MsgFormat(ResponseCode.成功, "可注册", "0 " + _userName);
}
}
private int UserInfoGetCount(string username)
{
//return Convert.ToInt32(SearchValue("select count(id) from userinfo where username='" + username + "'"));
return username == "admin" ? 1 : 0;
}
}
} public class ApiTools
{
private string msgModel = "{{\"code\":{0},\"message\":\"{1}\",\"result\":{2}}}";
public ApiTools()
{
}
public HttpResponseMessage MsgFormat(ResponseCode code, string explanation, string result)
{
string r = @"^(\-|\+)?\d+(\.\d+)?$";
string json = string.Empty;
if (Regex.IsMatch(result, r) || result.ToLower() == "true" || result.ToLower() == "false" || result == "[]" || result.Contains('{'))
{
json = string.Format(msgModel, (int)code, explanation, result);
}
else
{
if (result.Contains('"'))
{
json = string.Format(msgModel, (int)code, explanation, result);
}
else
{
json = string.Format(msgModel, (int)code, explanation, "\"" + result + "\"");
}
}
return new HttpResponseMessage { Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json") };
}
}我用postman选择POST,输入网址及入参:
http://localhost:49449/ActionApi/UserInfo/CheckUserName?username=ZHANGSAN
结果得到的结果是这个,真是太奇怪了。
{
"version": {
"major": 1,
"minor": 1,
"build": -1,
"revision": -1,
"majorRevision": -1,
"minorRevision": -1
},
"content": {
"headers": [
{
"key": "Content-Type",
"value": [
"application/json; charset=utf-8"
]
}
]
},
"statusCode": 200,
"reasonPhrase": "OK",
"headers": [],
"requestMessage": null,
"isSuccessStatusCode": true
}
...全文
相关推荐
HotelListing
同时,可以使用ASP.NET Core框架构建Web API服务,提供RESTful接口。Entity Framework Core作为ORM(对象关系映射)工具,可以简化数据库操作。
ASP.NET Core Web API,具有单个控制器的多个Get或Post方法
在ASP.NET Core中,Web API控制器是实现HTTP操作(如GET、POST等)的主要组件。
Asp.Net Core使用swagger生成api文档的完整步骤
在Asp.Net Core中,Swagger是一个强大的工具,用于生成API文档,它可以帮助开发者轻松地创建、测试和理解API接口。
使用ASP.NET Core创建API(第2天):在ASP.NET Core中创建API
在本教程中,我们将深入探讨如何使用ASP.NET Core创建API,这是第二天的学习内容。
ASP.NET-Core-REST-API:ASP.NET Core REST API的入门项目
在ASP.NET Core中,我们通过控制器类和HTTP动词(GET、POST、PUT、DELETE等)来定义资源的操作。


