Question

In IResponseProcessor.Process

string format = context.Request.Query["format"].Value ?? "json";
format = format.ToLower();
Response response;
switch (format)
{
    case "protbuf":
        response = // balabalabala
    break;
    case "yaml":
        response = // balabalabala
    break;
    case "xml":
        response = // balabalabala
    break;
    case "jsonp":
        response = // balabalabala
    break;
    case "json":
    default:
        response = new JsonResponse(apiResponse, new DefaultJsonSerializer());
        break;
}
if (context.Response != null)
{
    // for test
    context.Response.Cookies.Add(new NancyCookie("wener", "cool"));
    //response.Cookies.InjectFrom(context.Response.Cookies);
    //response.Headers.InjectFrom(context.Response.Headers);
}
return response;

ValueInjecter can not deal with List,because the NancyCookie dose not hava parameter-less ctor,I can not use this neither.

It is possible to copy cookie and header without foreach ? and how ?

Was it helpful?

Solution

I asked you on JabbR but we didn't catch each others questions and answers. :)

So to clarify, you want to preserve the cookies when you output a new response.

The problem is not adding the cookies to the response, you need to change the way you do responses.

Nancy handles content negotiation for you by default.

This means if you pass the header accepts with json/application, Nancy will see this and automatically send you JSON.

There is also a way to force JSON if you cannot change the headers, this is done by appending the type to the query.

Adding .json to the end of your URL will make nancy return JSON.

This is the same when using text/xml for the accepts and .xml for the URL, will return you XML.


If your request always returns JSON, you can do something like:

return response.AsJson(apiResponse);

This mean you're returning the existing response rather than creating a new one. All cookies will be attached, etc.

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top