0
votes

I'm using .NET Web API and I wish to send the newly created cookie along with the string which was generated in the Web API.

The C# Code:

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);

}

How could I send the response "Cookie Created Successfully !" along with the Cookie serverCookie

Kindly assist me how to send these two in a single response to the client. I'm getting the 500 Internal Server Error

I seen the following message in the Response

{"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)"}}}

1
Have you run it under a debugger to see what exception is thrown?fahadash
@fahadash In Web API, it returns without any exception. I pasted the response. Kindly check it once.user6060080

1 Answers

2
votes

You are returning an unsupported type. I am sure that the framework will never be able to serialize Tuple<HttpResponseMessage, string>. If you want to modify the response headers you need to return just HttpResponseMessage.

This should work as you expect:

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;
}