ASP.NET Core API控制器通常返回显式类型(默认情况下,如果您创建一个新项目),如下所示:
[Route("api/[controller]")]
public class ThingsController : Controller
{
// GET api/things
[HttpGet]
public async Task<IEnumerable<Thing>> GetAsync()
{
//...
}
// GET api/things/5
[HttpGet("{id}")]
public async Task<Thing> GetAsync(int id)
{
Thing thingFromDB = await GetThingFromDBAsync();
if(thingFromDB == null)
return null; // This returns HTTP 204
// Process thingFromDB, blah blah blah
return thing;
}
// POST api/things
[HttpPost]
public void Post([FromBody]Thing thing)
{
//..
}
//... and so on...
}
Run Code Online (Sandbox Code Playgroud)
问题是return null;- 它返回HTTP 204:成功,没有内容.
然后,许多客户端Javascript组件将其视为成功,因此代码如下:
const …Run Code Online (Sandbox Code Playgroud) c# http-status-code-404 asp.net-core-mvc asp.net-core asp.net-core-webapi
我试图按照此链接中的建议向控制器调用返回错误, 以便客户端可以采取适当的操作.控制器由javascript通过jquery AJAX调用.只有在我没有将状态设置为错误时才会返回Json对象.这是示例代码
if (response.errors.Length > 0)
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(response);
Run Code Online (Sandbox Code Playgroud)
如果我没有设置状态代码,我会得到Json.如果我设置了状态代码,我会返回状态代码但不会返回Json错误对象.
更新 我想将一个Error对象作为JSON发送,以便可以处理ajax的错误回调.
我使用MVC4 web-api,c#,并希望使用Json.net返回Json .
问题是它带有"反斜杠".
我还将此代码添加到Global.asax.
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
Run Code Online (Sandbox Code Playgroud)
这是它的回报.
"{\"cid\":1,\"model\":\"WT50JB\",\"detail\":\"sdf??\",\"unit\":2,\"time_in\":\"2012-12-11T19:00:00\",\"time_out\":\"2012-12-12T13:00:06.2774691+07:00\",\"time_used_dd\":0.0,\"time_used_hh\":0.0}"
Run Code Online (Sandbox Code Playgroud)
所以我想看到的是
{"cid":1,"model":"WT50JB","detail":"sdf??","unit":2,"time_in":"2012-12-11T19:00:00","time_out":"2012-12-12T13:08:50.5444555+07:00","time_used_dd":0.0,"time_used_hh":0.0}
Run Code Online (Sandbox Code Playgroud)
这是JsonConvertor
string json = JsonConvert.SerializeObject(myObj);
Run Code Online (Sandbox Code Playgroud) 我见过很多使用默认AddMvc()服务的ASP.NET Core Web API项目,但没有意识到AddMvcCore()由于对服务的控制,使用是一种优越的选择.
究竟如何实现通过使用ASP.NET核心的Web API AddMvcCore()和它为什么好?
我进行了 AJAX 调用,将图像文件发送到我的 Web 服务 (.asmx) 方法之一。一切都很好,但问题是 Web 服务返回 XML 而不是 JSON,因为我必须设置为'contentType'' false',否则文件无法发送。(如果我设置contentType为application/json; charset=utf-8,它会返回 JSON,但我不能这样做,因为我正在发送一个文件。)
这是我的 JavaScript:
function setAvatar(imageFile, successCallback) {
var formData = new FormData();
formData.append("UploadedAvatar", imageFile);
$.ajax({
type: "POST",
url: "/Services/UserService.asmx/SetAvatar",
contentType: false,
processData: false,
dataType: 'json',
data: formData,
success: function (result) {
alert(result.d);
alert(result.d.IsSuccessful);
if (typeof successCallback === 'function')
successCallback(result);
}
});
Run Code Online (Sandbox Code Playgroud)
以及网络服务方法:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Result SetAvatar()
{
HttpPostedFile postedFile = HttpContext.Current.Request.Files["UploadedAvatar"];
Image avatar = Image.FromStream(postedFile.InputStream, true, …Run Code Online (Sandbox Code Playgroud) c# ×4
asp.net-core ×2
json ×2
ajax ×1
asp.net ×1
asp.net-mvc ×1
javascript ×1
jquery ×1
web-services ×1