Exemplo n.º 1
0
        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler" /> interface.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext" /> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
        /// <exception cref="System.Web.HttpException">404;Not found</exception>
        public void ProcessRequest(HttpContext context)
        {
            if (this.FileExists(context.Request.Url.AbsolutePath))
            {
                using (var fileStream = this.OpenFile(context.Request.Url.AbsolutePath))
                {
                    var fileName = VirtualPathUtility.GetFileName(context.Request.Url.AbsolutePath);
                    var buffer   = new byte[fileStream.Length];
                    fileStream.Read(buffer, 0, (int)fileStream.Length);
                    context.Response.ContentType = ResourceHttpHandler.GetMimeMapping(fileName);

                    if (fileName.EndsWith("css", StringComparison.OrdinalIgnoreCase) ||
                        fileName.EndsWith("js", StringComparison.OrdinalIgnoreCase) ||
                        fileName.EndsWith("html", StringComparison.OrdinalIgnoreCase) ||
                        fileName.EndsWith("htm", StringComparison.OrdinalIgnoreCase))
                    {
                        var cache = context.Response.Cache;
                        cache.SetCacheability(HttpCacheability.Public);
                        cache.SetExpires(DateTime.Now + TimeSpan.FromDays(7));
                        cache.SetValidUntilExpires(true);

                        var lastWriteTime = ResourceHttpHandler.GetAssemblyLastWriteTime();
                        cache.SetLastModified(lastWriteTime);
                    }

                    this.WriteToOutput(context, buffer);
                }
            }
            else
            {
                throw new HttpException(404, "Not found");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sets client cache for the requested resource. In debug mode this caching is disabled.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="fileName">Name of the file.</param>
        protected virtual void SetResponseClientCache(HttpContext context, string fileName)
        {
#if !DEBUG
            if (fileName.EndsWith(".css", StringComparison.OrdinalIgnoreCase) ||
                fileName.EndsWith(".js", StringComparison.OrdinalIgnoreCase) ||
                fileName.EndsWith(".html", StringComparison.OrdinalIgnoreCase) ||
                fileName.EndsWith(".htm", StringComparison.OrdinalIgnoreCase) ||
                fileName.EndsWith(".sf-cshtml", StringComparison.OrdinalIgnoreCase))
            {
                var cache = context.Response.Cache;
                cache.SetCacheability(HttpCacheability.Public);
                cache.SetExpires(DateTime.Now + TimeSpan.FromDays(7));
                cache.SetValidUntilExpires(true);
                var lastWriteTime = ResourceHttpHandler.GetAssemblyLastWriteTime();
                cache.SetLastModified(lastWriteTime);
            }
#endif
        }