我正在使用.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 Internal Server Error
我在Response中看到了以下消息
{“Message”:“发生错误 . ”,“ExceptionMessage”:“'ObjectContent`1'类型无法序列化内容类型'application / json; charset = utf-8'的响应正文 . ”,“ ExceptionType“:”System.InvalidOperationException“,”StackTrace“:null,”InnerException“:{”消息“:”发生了错误 . “,”ExceptionMessage“:”从'System.Version'上的'SeparatorsArray'获取值时出错“ . “,”ExceptionType“:”Newtonsoft.Json.JsonSerializationException“,”StackTrace“:”at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter)在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract)中的writer,Object value,JsonContainerContract contract,JsonProperty成员,JsonProperty属性,JsonContract&memberContract,Object&memberValue)\ r \ n ,J在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .SerializeObject (JsonWriter编写器,Object值,JsonObjectContract Contract ,JsonProperty成员,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty) containerProperty)\ r \ n在Newtonoft.Json.Serialization.JsonSerializerInternalWriter .SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n at Newtonso ft.Json.Serialization.JsonSerializerInternalWriter .SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .Serialize(JsonWriter jsonWriter,Object value)\ r \ n at Newoftoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,Object value)\ r \ n在Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter,Object value)\ r \ n在System.Net.Http.Formatting . JsonMediaTypeFormatter . <> c__DisplayClassd . b__c()\ r \ n在System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action,CancellationToken token)“,”InnerException“:{”Message“:”发生了错误 . “,”ExceptionMessage“:”公共语言运行时检测到一个无效的程序 . “,”ExceptionType“:”System.InvalidProgramException“,”StackTrace“:”at GetSeparatorsArray(Object)\ r \ n在Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)“}}}
1 回答
您将返回不受支持的类型 . 我确信该框架永远无法序列化
Tuple<HttpResponseMessage, string>
. 如果要修改响应头,则只需返回HttpResponseMessage
.这应该按预期工作: