【问题标题】:Return Cookie along with Object from .NET Web API从 .NET Web API 返回 Cookie 和对象
【发布时间】:2016-10-16 12:04:36
【问题描述】:

我正在使用 .NET Web API,我希望将新创建的 cookie 连同在 Web API 中生成的字符串一起发送。

C# 代码:

public Tuple<HttpResponseMessage, string> CookieMessage()
{
        string result = "Cookie Created Successfully !";

        CookieHeaderValue serverCookie = new CookieHeaderValue("", "");

        serverCookie = new CookieHeaderValue
                                ("IEM", "Success");
        serverCookie.Expires = DateTimeOffset.Now.AddMinutes(15);
        serverCookie.Domain = Request.RequestUri.Host;
        serverCookie.Path = "/";


    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);

    response.Headers.AddCookies(new CookieHeaderValue[] { serverCookie });

    return Tuple.Create(response, result);

}

我如何发送响应 "Cookie Created Successfully !" 以及 Cookie serverCookie

请帮助我如何将这两个发送给客户。我收到 500 内部服务器错误

我在响应中看到以下消息

{"Message":"发生错误。","ExceptionMessage":"The “ObjectContent`1”类型无法序列化响应正文 内容类型'应用程序/json; charset=utf-8'.","ExceptionType":"System.InvalidOperationException" ,"StackTrace":null,"InnerException":{"Message":"一个错误有 发生。","ExceptionMessage":"获取值时出错 'SeparatorsArray' 开启 'System.Version'。","ExceptionType":"Newtonsoft.Json.JsonSerializationException" “堆栈跟踪”:“在 Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object 目标)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer,对象值,JsonContainerContract 合约,JsonProperty 成员、JsonProperty 属性、JsonContract & memberContract、Object& 成员值)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .SerializeObject(JsonWriter writer, Object value, JsonObjectContract 合同,JsonProperty 成员,JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .SerializeValue(JsonWriter writer, Object value, JsonContract valueContract,JsonProperty 成员,JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .SerializeObject(JsonWriter writer, Object value, JsonObjectContract 合同,JsonProperty 成员,JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .SerializeValue(JsonWriter writer, Object value, JsonContract valueContract,JsonProperty 成员,JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .SerializeObject(JsonWriter writer, Object value, JsonObjectContract 合同,JsonProperty 成员,JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .SerializeValue(JsonWriter writer, Object value, JsonContract valueContract,JsonProperty 成员,JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .Serialize(JsonWriter jsonWriter, 对象值)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal (JsonWriter jsonWriter,对象值)\r\n at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter , 对象值)\r\n at System.Net.Http.Formatting.JsonMediaTypeFormatter.c__DisplayClassd。

b__c()\r\n 在 System.Threading.Tasks.TaskHelpers.RunSynchronously(动作动作, CancellationToken token)","InnerException":{"Message":"一个错误有 发生。","ExceptionMessage":"公共语言运行时检测到一个 无效的 程序。","ExceptionType":"System.InvalidProgramException","StackTrace":" 在 GetSeparatorsArray (Object )\r\n at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object 目标)"}}}

【问题讨论】:

  • 你有没有在调试器下运行过,看看抛出了什么异常?
  • @fahadash 在 Web API 中,它返回没有任何异常。我粘贴了回复。请检查一次。

标签: c# cookies asp.net-web-api asp.net-web-api2 httpcookie


【解决方案1】:

您正在返回不受支持的类型。我确信框架永远无法序列化Tuple&lt;HttpResponseMessage, string&gt;。如果要修改响应标头,只需返回 HttpResponseMessage

这应该可以如您所愿:

public HttpResponseMessage CookieMessage()
{
    var response = Request.CreateResponse<string>(
        HttpStatusCode.OK,
        "Cookie Created Successfully !"
    );

    var cookie = new CookieHeaderValue("IEM", "Success");
    cookie.Expires = DateTimeOffset.Now.AddMinutes(15);
    cookie.Domain = Request.RequestUri.Host;
    cookie.Path = "/";

    response.Headers.AddCookies(new CookieHeaderValue[] { cookie });

    return response;
}

【讨论】:

    猜你喜欢
    • 2021-10-29
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 2019-04-22
    • 2015-08-27
    • 1970-01-01
    相关资源
    最近更新 更多