How to cache a page on IIS and client? - asp.net-mvc

i want to cache my page and use this type of caching
[OutputCache(Duration = 21600, VaryByParam = "none")]
public ActionResult Index()
{
//some codes
return View();
}
but i 'm confused this type of cache will stored on IIS or on client browser?
how can i cache my page on user browser not on the server?

Default asp.net cache pages everywhere it can (server, proxy, client). You can change this by attribute
[Output(Location=OutputCacheLocation.Any)]
You can set the Location property to any one of the following values:
OutputCacheLocation·Any
OutputCacheLocation·Client
OutputCacheLocation.Downstream
OutputCacheLocation.Server
OutputCacheLocation.None
OutputCacheLocation.ServerAndClient

Related

OutputCache Not working on Page Load(Reload)

In my project of MVC I am using OutputCache to cache page on both server and client side. Whenever I load page or reload page data is not coming from cache. Is there any explanation for this?
Controller:Main.cs
[OutputCache(Duration=3600)]
public ActionResult Main()
{
//some code
return View("~/Views/main.cshtml",DateTime.Now);
}
View:main.cshtml
#model DateTime
<h2>Hi</h2>
<h2>Hello</h2>
<h2>#Model</h2>

C # MVC view files only for logged in users

how can I set about c # MVC viewing files with absolute path (eg. www.mysite.it/namefile.pdf) only for authenticated users ? for authentication use the method FormsAuthentication.Authenticate(). thanks for the support.
I think that the more properly way to do that is:
www.mysite.it/f={filename}
And in your controller you use the [Authorize] to check if user is authenticated. If the user is authenticated you allow him to view, or download, the file.
The following code can help you to understand:
//Ensure that the user is authenticated
[Authorize]
public class HomeController : Controller
{
string DefaultFileFolder = "C:/Files";
public ActionResult Index(string f)
{
//File request
if (!String.IsNullOrWhiteSpace(f))
{
var filePath = System.IO.Path.Combine(DefaultFileFolder, f);
var mimeType = "text/plain";
return File(filePath, mimeType);
}
return View();
}
}
As far as I know the only way to do this is to route requests for static content through ASP.NET. Normally you don't do this as IIS by itself is far more efficient at serving these types of resources.
From this excellent article, you can force static content requests to go through ASP.NET by:
On IIS 7 this is done either by setting
runAllManagedModulesForAllRequests=”true” or removing the
"managedHandler" preCondition for the UrlRoutingModule.
Depending on your needs, you may wish to do something more in line with what Richard suggested. Something like this perhaps?

Consuming a click-once application from an MVC controller/action

I am using an MVC controller/action located in /myServer/myArea/MyClickOnce/Open that returns a FileResult
public class MyClickOnceController : Controller
{
public FileResult Open()
{
FilePathResult file = new FilePathResult("/Provisioning/4843EA3F-9138-4A0D-9D33-BF4CDDEB7C7E/MyClickOnce.application", "application/x-ms-application");
return file;
}
}
this works fine for the initial loading, but then click-once makes a subsequent request to:
/myServer/myArea/MyClickOnce/9.0.0.132/MyClickOnce.exe.manifest
This path doesn't actually exist because the physical path for the click once is in:
/Provisioning/4843EA3F-9138-4A0D-9D33-BF4CDDEB7C7E/*
so it exists here:
/Provisioning/4843EA3F-9138-4A0D-9D33-BF4CDDEB7C7E/9.0.0.132/MyClickOnce.exe.manifest
Should I be using routing to redirect all these subsequent requests? Is there a better approach to consume a click-once application from and MVC controller/action?
If you use a RedirectResult, the subsequent requests to the ClickOnce manifest and other files get routed to the correct directory.
public ActionResult Open()
{
string path = "/Provisioning/4843EA3F-9138-4A0D-9D33-BF4CDDEB7C7E/MyClickOnce.application";
return new RedirectResult(path);
}

Reading cookies (on the server) written by a different host on the same domain

In one ASP.NET MVC app on a domain "mysite.com", I'm writing a cookie to a specific domain, say ".mysite.com". I'm able to confirm that the browser accepts my cookie.
Then, from another ASP.NET MVC app, say "jmp.mysite.com", I'm trying to read the cookie set by the first app.
The problem? Well, I can't read the cookie. My browser says it's there, but my web server says it isn't.
Is there some special way of reading these sorts of cookies? Could IIS perhaps not be sending them to ASP.NET?
To create the cookie from foo.mysite.com:
public ActionResult Index()
{
var cookie = new HttpCookie("foo", "bar")
{
HttpOnly = true,
Domain = "mysite.com"
};
return View();
}
and to read the cookie from jmp.mysite.com:
public ActionResult Index()
{
var cookie = Request.Cookies["foo"];
if (cookie != null)
{
var value = cookie.Value;
// TODO: do something with the value
}
return View();
}

How to clear/expire browser cache on log off?

In my ASP.net MVC application, I've got several views that I'd like to set to save in the browser's cache. I've got the methods built to do it, but here's my issue.
The menu in my site is different between logged in and logged off visitors. If the logged in page is cached, then even when the user logs off the menu remains in the logged in mode. It's actually not, but on that visitor's browser it is.
How can I go about clearing/expiring that cache so the visitor's browser updates when I need it to, yet still be able to make use of browser cache?
Thanks in advance!
For HTML pages it's difficult. I turned off client caching for that same reason, and tried to make the server caching as efficient as possible. I now use OutputCache with VaryByCustom set to the login status.
We ran some load tests on that system and the only bottleneck is the bandwidth that this generates.
And on a side note: I used donut-caching for the login status. But I was not able to get it to work with dynamic compression (to reduce the bandwidth bottleneck mentioned above)
See also this question
You can do it with an AutoRefresh attribute on your action method. Here are some examples:
[AutoRefresh(ControllerName = "Home", ActionName = "About", DurationInSeconds = 10)]
public ActionResult Index1()
{
}
AutoRefresh(ActionName = "About", DurationInSeconds = 15)]
public ActionResult Index2()
{
}
[AutoRefresh(RouteName = "ByFavoriteRoute", DurationInSeconds = 30)]
public ActionResult Index3()
{
}
[AutoRefresh(DurationInSeconds = 45)]
public ActionResult Index4()
{
}

Resources