I have a very strange behavior in my MVC 5 application. This only happens when the connection is a mobile connection like 3G or GPRS. On a Wifi connection all works well.
The application is hosted on iis 7.5. It uses Form authentication. The app is developed in .Net 4.5 MVC 5.
In my view I use Razor html helpers and it seems that IIS cache the views with these helper values and if the connection is slow like GPRS it renders these cached views with the wrong values.
One example is: @System.Web.HttpContext.Current.User.Identity.Name
In my _Layout view I use the @System.Web.HttpContext.Current.User.Identity.Name to display the username in the right corner of the site:
<span>
<img class="StatusImage" src='@Url.Content("~/Content/images/User16.png")' />
@System.Web.HttpContext.Current.User.Identity.Name
</span>
If the connection is slow, IIS renders a other username than what the user logged in with.
Another obscure example is a situation where we display project names as buttons. These buttons are dynamically created according to data in the database.
@foreach (var item in Model.List)
{
<div>
<a class="linkButtonLarge" href="@Url.Action("ProjectIndex", "AssetCapture", new { ProjID = @item.ID })" >
<img src="~/Content/images/ProjectItem.png" />@item.Name
</a>
</div>
}
So if there are three items in the list, then three buttons will be displayed, each one with its unique href. When a button is clicked the URL request will be for instance
http://www.example.com/AssetCapture/ProjectIndex/?ProjID=PROJ1
The controller Action rendering the view looks like this:
public ActionResult ProjectIndex(string ProjID)
{
ModelState.Clear();
Models.ProjectModel model = new Models.ProjectModel();
model.SelectedProject = ProjID;
return View("ProjectIndex", model);
}
On a slow connection like GPRS the value @Model.SelectedProject is not always correct in the view that is rendered.
Lets say the user clicked on the button with value "PROJ1" the URL in the URL box will show up as
http://www.example.com/AssetCapture/ProjectIndex/?ProjID=PROJ1
but @Model.SelectedProject will show "PROJ2" when the page is rendered. It seems like a other user selected "PROJ2" previously and that view was cached. Now that the user on a poor connection try to access the same view with a different parameter "PROJ1" it renders the cached version of "PROJ2".
I think it is an IIS issue, but it could be something else. If anyone can help I would really appreciate it.
EDIT
Here is the Request Header when connected through Wi-Fi:
GET http://www.ams360-tlokwe.co.za/AssetCapture/ProjectIndex/?ProjID=PROJ2 >HTTP/1.1 Host: www.ams360-tlokwe.co.za Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36 Referer: http://www.ams360-tlokwe.co.za/AssetCapture/Project/ Accept-Encoding: gzip, deflate, sdch Accept-Language: en-GB,en-US;q=0.8,en;q=0.6 Cookie: ASP.NET_SessionId=oonansf0eotsv1z13ey2ukkr; AspxAutoDetectCookieSupport=1; .ASPXAUTH=60026C569909566F84B004A4E06671ED9ECA88F279D25E55D7A283A7E732D6E50EE5B0401C1D3EE2BDF5A9F828CD5B56891C2EB0C05BE007F3D43DBD7923D5D384AE59C16EE8043766176F46162387073090F344E2CB799843FED12946ADE1E90D8F978E1DDEC91997E618655B4D520EE0DE467C52DD905C000C4C7B0C8C2558
And here is the Request Header when connected through the Mobile Connection:
GET http://www.ams360-tlokwe.co.za/AssetCapture/ProjectIndex/?ProjID=PROJ2 HTTP/1.1 Host: www.ams360-tlokwe.co.za Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36 Referer: http://www.ams360-tlokwe.co.za/AssetCapture/Project/ Accept-Encoding: gzip, deflate, sdch Accept-Language: en-GB,en-US;q=0.8,en;q=0.6 Cookie: ASP.NET_SessionId=oonansf0eotsv1z13ey2ukkr; AspxAutoDetectCookieSupport=1; .ASPXAUTH=60026C569909566F84B004A4E06671ED9ECA88F279D25E55D7A283A7E732D6E50EE5B0401C1D3EE2BDF5A9F828CD5B56891C2EB0C05BE007F3D43DBD7923D5D384AE59C16EE8043766176F46162387073090F344E2CB799843FED12946ADE1E90D8F978E1DDEC91997E618655B4D520EE0DE467C52DD905C000C4C7B0C8C2558
They are exactly the same.
Response Headers
From a WiFI connection:
HTTP/1.1 200 OK
Cache-Control: public, no-store, max-age=0, s-maxage=0
Content-Type: text/html; charset=utf-8
Expires: Mon, 30 Mar 2015 07:18:51 GMT
Last-Modified: Mon, 30 Mar 2015 07:18:51 GMT
Vary: *
Server: Microsoft-IIS/7.5
Access-Control-Allow-Origin: *
X-AspNetMvc-Version: 5.2
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 30 Mar 2015 07:18:51 GMT
Content-Length: 136803
And from the Mobile Connection
HTTP/1.1 200 OK
Vary: User-Agent
Cache-Control: public, no-store, max-age=0, s-maxage=0
Content-Type: text/html; charset=utf-8
Expires: Mon, 30 Mar 2015 07:20:09 GMT
Last-Modified: Mon, 30 Mar 2015 07:20:09 GMT
Vary: *
Server: Microsoft-IIS/7.5
Access-Control-Allow-Origin: *
X-AspNetMvc-Version: 5.2
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 30 Mar 2015 07:20:09 GMT
Content-Length: 54498
EDIT2
The Response Header is actually different when on a mobile (3G) connection. It returns with a Transfer-Encoding: chunked mode. Fiddler gives you an option
Response is encoded and may need to be decoded before inspection.Click here to transform
If I click on this the response header change from
Transfer-Encoding: chunked mode
to
Content-Length: 54498