Zoran Knezevic - 457 - Apr 4, 2014

Hey Guys, I am using SS with the Razor plugin to serve HTML pages and JSON from the same service.  If I make a basic request from the browser I do get the HTML as expected.  When I make the call from JavaScript and request JSON I get JSON.  However, when I am on a page and I click the Back button in the browser, I get JSON for the previous page instead of HTML, which is not what I want.  Has anyone else seen this? Is this a bug, or is this the expected result?  If it is expected, then is there some setting to override it?  Thanks!

You can use [AddHeader(ContentType = MimeTypes.Html)] filter attribute to force html.

Do you have an example of the HTTP request it’s sending that returns JSON?

Zoran Knezevic:

I was using Fiddler to capture the traffic.  All actual requests returned what I expected.  However, after I navigated a few of the pages, and then used the Back and Forward buttons in Chrome, I only saw the JSON results in the browser.  However, those must have been cached, as Fiddler was not picking up any additional requests as I was navigating back and forth.

Can you clear the caches? just want to see what the Request headers were that’s returning the JSON response.

Actually it might be because you’re making another JSON call with the same url (i.e. just differentiated by using the Accept header) and Chrome will assume the cache is valid for all requests with different accept headers.

If that’s the case setting the “Vary: Accept” header will tell Chrome to keep different caches based on different accept request, e.g: 

SetConfig(new HostConfig {
    GlobalResponseHeaders = {
        {“Vary”,“Accept”}
    }
});

I might add this as a default Global Response Header since content negotiation is a core feature of SS.

Zoran Knezevic:

I am using the same URL for both requests.  So, I bet your last comment is dead-on.  I will try the “Vary” “Accept” setting first thing on Monday and let you know.  Thanks!

Zoran Knezevic:

Just an FYI, adding the GlobalResponseHeaders got me a step closer but did not solve my whole problem.  After I added it, then I was not automatically getting the JSON result when I clicked back, instead clicking the back button would generate another GET request that would hit the server.  But then when the AJAX call (another GET to the same URL) was executed, it would not hit the server, instead Chrome would automatically return the HTML response even though the “dataType” option was set to “json”.  However, since in all of my ajax requests I expect JSON in return, I decided to add the “?format=json” to every GET Url, and therefore even though I am hitting the same base URL all ajax requests are different, so I  get back the correct cached version.