Disable browser cache for entire ASP.NET website

Create a class that inherits from IActionFilter. public class NoCacheAttribute : ActionFilterAttribute { public override void OnResultExecuting(ResultExecutingContext filterContext) { filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1)); filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false); filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches); filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache); filterContext.HttpContext.Response.Cache.SetNoStore(); base.OnResultExecuting(filterContext); } } Then put attributes where needed… [NoCache] [HandleError] public class AccountController : Controller { [NoCache] [Authorize] public ActionResult ChangePassword() { return View(); } }

Better way to find control in ASP.NET

If you’re looking for a specific type of control you could use a recursive loop like this one – http://weblogs.asp.net/eporter/archive/2007/02/24/asp-net-findcontrol-recursive-with-generics.aspx Here’s an example I made that returns all controls of the given type /// <summary> /// Finds all controls of type T stores them in FoundControls /// </summary> /// <typeparam name=”T”></typeparam> private class ControlFinder<T> where … Read more

ASP.NET “special” tags

The official name is “server-side scripting delimiters” or “ASP.NET inline expressions“. Visual Studio 2008 syntax highlighting settings dialog calls these “HTML Server-Side Script”. Microsoft guys call them “code nuggets” in their blogs. <%@ %> is a Directive for ASP.NET Web Pages. Used for pages and controls to configure page/control compiler settings (<%@ Control Inherits=”MyParentControl” %>). … Read more

Compile Views in ASP.NET MVC

From the readme word doc for RC1 (not indexed by google) ASP.NET Compiler Post-Build Step Currently, errors within a view file are not detected until run time. To let you detect these errors at compile time, ASP.NET MVC projects now include an MvcBuildViews property, which is disabled by default. To enable this property, open the … Read more

Can an ASP.NET MVC controller return an Image?

Use the base controllers File method. public ActionResult Image(string id) { var dir = Server.MapPath(“/Images”); var path = Path.Combine(dir, id + “.jpg”); //validate the path for security or use other means to generate the path. return base.File(path, “image/jpeg”); } As a note, this seems to be fairly efficient. I did a test where I requested … Read more

Can the ViewBag name be the same as the Model property name in a DropDownList?

You should not use the same name for the model property and the ViewBag property (and ideally you should not be using ViewBag at all, but rather a view model with a IEnumerable<SelectListItem> property). When using @Html.DropDownListFor(m => m.CustomerId, ….) the first “Please Select” option will always be selected even if the value of the … Read more

Server.MapPath(“.”), Server.MapPath(“~”), Server.MapPath(@”\”), Server.MapPath(“/”). What is the difference?

Server.MapPath specifies the relative or virtual path to map to a physical directory. Server.MapPath(“.”)1 returns the current physical directory of the file (e.g. aspx) being executed Server.MapPath(“..”) returns the parent directory Server.MapPath(“~”) returns the physical path to the root of the application Server.MapPath(“https://stackoverflow.com/”) returns the physical path to the root of the domain name (is … Read more