相关疑难解决方法(0)

ASP.NET Core返回带有状态代码的JSON

我正在寻找在我的.NET Core Web API控制器中使用HTTP状态代码返回JSON的正确方法.我用它像这样:

public IHttpActionResult GetResourceData()
{
    return this.Content(HttpStatusCode.OK, new { response = "Hello"});
}
Run Code Online (Sandbox Code Playgroud)

这是一个4.6 MVC应用程序,但现在随着.NET核心我似乎没有这个IHttpActionResult我已经ActionResult和使用这样的:

public ActionResult IsAuthenticated()
{
    return Ok(Json("123"));
}
Run Code Online (Sandbox Code Playgroud)

但是来自服务器的响应很奇怪,如下图所示:

在此输入图像描述

我只是希望Web API控制器返回带有HTTP状态代码的JSON,就像我在Web API 2中所做的那样.

c# json asp.net-core asp.net-core-webapi

119
推荐指数
6
解决办法
20万
查看次数

asp.net asmx web服务返回xml而不是json

为什么这个简单的Web服务拒绝将JSON返回给客户端?

这是我的客户端代码:

        var params = { };
        $.ajax({
            url: "/Services/SessionServices.asmx/HelloWorld",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            timeout: 10000,
            data: JSON.stringify(params),
            success: function (response) {
                console.log(response);
            }
        });
Run Code Online (Sandbox Code Playgroud)

和服务:

namespace myproject.frontend.Services
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    public class SessionServices : System.Web.Services.WebService
    {
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

web.config中:

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)

并回应:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>
Run Code Online (Sandbox Code Playgroud)

无论我做什么,响应总是以XML形式返回.如何让Web服务返回Json?

编辑:

这是Fiddler HTTP跟踪: …

c# asp.net json web-services asmx

50
推荐指数
4
解决办法
7万
查看次数