Return 来自 .NET Web 的 Cookie 和对象 API

Return Cookie along with Object from .NET Web API

我正在使用 .NET Web API,我希望将新创建的 cookie 与在 Web API 中生成的字符串一起发送 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":"An error has occurred.","ExceptionMessage":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.","ExceptionType":"System.InvalidOperationException" ,"StackTrace":null,"InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Error getting value from 'SeparatorsArray' on 'System.Version'.","ExceptionType":"Newtonsoft.Json.JsonSerializationException" ,"StackTrace":" at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer , Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract & memberContract, Object& memberValue)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .Serialize(JsonWriter jsonWriter, Object value)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal (JsonWriter jsonWriter, Object value)\r\n at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter , Object value)\r\n at System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c__DisplayClassd.

b__c()\r\n at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Common Language Runtime detected an invalid program.","ExceptionType":"System.InvalidProgramException","StackTrace":" at GetSeparatorsArray (Object )\r\n at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)"}}}

您正在 return 使用不受支持的类型。我确信框架永远无法序列化 Tuple<HttpResponseMessage, string>。如果你想修改响应头,你需要 return 只需 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;
}