user2217261
user2217261

Reputation: 455

Different HttpStatusCode for different clients in Nancy

After navigate to http://localhost:57645 code below returns 200 (OK) for DHC by Restlet and 500 (InternalServerError) for "Chrome Browser".

public class SampleModule : Nancy.NancyModule
{
    public SampleModule()
    {
        Get["/"] = _ =>
        {
            return Negotiate
                .WithStatusCode(HttpStatusCode.OK)
                .WithModel(10);
        };
}

How is this possible?

Upvotes: 0

Views: 243

Answers (2)

Phill
Phill

Reputation: 18794

The behavior you're experiencing in the first example is expected. The browser is requesting text/html

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Nancy is attempting to locate a view based on the type 10, since 10 is a type of Int32, Nancy is trying to find a view called Int32 since its trying to response with a View, since that's what the browser asked for.

The Dev Client you're using in Chrome is most likely sending a request for JSON by default which is why it appears to be working.

You can read more about this here:

http://www.philliphaydon.com/2013/04/nancyfx-revisiting-content-negotiation-and-apis-part-1/

Upvotes: 2

user2217261
user2217261

Reputation: 455

Found it! In my case best to use

return Response.AsJson(10);

Upvotes: 0

Related Questions