我使用的是.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内部服务器错误
我在响应中看到了以下消息
{&#34;消息&#34;:&#34;发生了错误。&#34;,&#34; ExceptionMessage&#34;:&#34; The &#39; ObjectContent`1&#39;类型无法序列化响应正文 内容类型&#39; application / json; 字符集= UTF-8&#39;&#34;&#34; ExceptionType&#34;:&#34; System.InvalidOperationException&#34; ,&#34; StackTrace&#34;:null,&#34; InnerException&#34;:{&#34; Message&#34;:&#34;错误有 发生了。&#34;,&#34; ExceptionMessage&#34;:&#34;从中获取价值时出错 &#39; SeparatorsArray&#39;上 。&#39; System.Version&#39;&#34;&#34; ExceptionType&#34;:&#34; Newtonsoft.Json.JsonSerializationException&#34; &#34;堆栈跟踪&#34;:&#34;在 Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(对象 目标)\ r \ n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer,Object value,JsonContainerContract contract,JsonProperty 成员,JsonProperty财产,JsonContract&amp; memberContract,Object&amp; memberValue)\ 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 .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,Object value)\ r \ n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,Object value)\ r \ n at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, 对象值)\ r \ n at System.Net.Http.Formatting.JsonMediaTypeFormatter&LT;&GT; c__DisplayClassd。
b__c()\ r \ n在System.Threading.Tasks.TaskHelpers.RunSynchronously(动作, CancellationToken token)&#34;,&#34; InnerException&#34;:{&#34; Message&#34;:&#34;错误有 发生了。&#34;,&#34; ExceptionMessage&#34;:&#34;公共语言运行时检测到 无效 。程序&#34;&#34; ExceptionType&#34;:&#34; System.InvalidProgramException&#34;&#34;堆栈跟踪&#34;:&#34; 在GetSeparatorsArray(Object)\ r \ n at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(对象 目标)&#34;}}}
答案 0 :(得分:2)
您将返回不受支持的类型。我确信该框架永远无法序列化Tuple<HttpResponseMessage, string>
。如果要修改响应标头,则只需返回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;
}