System.Web.StaticFileHandler.ProcessRequest C# (CSharp) Method

ProcessRequest() public method

public ProcessRequest ( HttpContext context ) : void
context HttpContext
return void
		public void ProcessRequest (HttpContext context)
		{
			HttpRequest request = context.Request;
			HttpResponse response = context.Response;

			if (HostingEnvironment.HaveCustomVPP) {
				VirtualFile vf = null;
				VirtualPathProvider vpp = HostingEnvironment.VirtualPathProvider;
				string vpath = request.FilePath;
				
				if (vpp.FileExists (vpath))
					vf = vpp.GetFile (vpath);

				if (vf == null)
					throw new HttpException (404, "Path '" + vpath + "' was not found.", vpath);

				response.ContentType = MimeTypes.GetMimeType (vpath);
				response.TransmitFile (vf, true);
				return;
			}
			
			string fileName = request.PhysicalPath;
			FileInfo fi = new FileInfo (fileName);
			if (!fi.Exists || !ValidFileName (fileName))
				throw new HttpException (404, "Path '" + request.FilePath + "' was not found.", request.FilePath);

			if ((fi.Attributes & FileAttributes.Directory) != 0) {
				response.Redirect (request.Path + '/');
				return;
			}
			
			string strHeader = request.Headers ["If-Modified-Since"];
			try {
				if (strHeader != null) {
					DateTime dtIfModifiedSince = DateTime.ParseExact (strHeader, "r", null);
					DateTime ftime;
#if TARGET_JVM
					try 
					{
						ftime = fi.LastWriteTime.ToUniversalTime ();
					} 
					catch (NotSupportedException) 
					{
						// The file is in a WAR, it might be modified with last redeploy.
						try {
							ftime = (DateTime) AppDomain.CurrentDomain.GetData (".appStartTime");
						}
						catch {
							ftime = DateTime.MaxValue;
						}
					}
#else
					ftime = fi.LastWriteTime.ToUniversalTime ();
#endif
					if (ftime <= dtIfModifiedSince) {
						response.StatusCode = 304;
						return;
					}
				}
			} catch { } 

			try {
				DateTime lastWT = fi.LastWriteTime.ToUniversalTime ();
				response.AddHeader ("Last-Modified", lastWT.ToString ("r"));
				response.ContentType = MimeTypes.GetMimeType (fileName);
				response.TransmitFile (fileName, true);
			} catch (Exception) {
				throw new HttpException (403, "Forbidden.");
			}
		}

Usage Example

Example #1
0
        public virtual IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback callback, object state)
        {
            this.Context = context;

            HttpRequest req      = context != null ? context.Request : null;
            string      filePath = req != null ? req.FilePath : null;

            if (!String.IsNullOrEmpty(filePath) && String.Compare(".asp", VirtualPathUtility.GetExtension(filePath), StringComparison.OrdinalIgnoreCase) == 0)
            {
                throw new HttpException(String.Format("Access to file '{0}' is forbidden.", filePath));
            }

            if (req != null && String.Compare("POST", req.HttpMethod, StringComparison.OrdinalIgnoreCase) == 0)
            {
                throw new HttpException(String.Format("Method '{0}' is not allowed when accessing file '{1}'", req.HttpMethod, filePath));
            }

            var sfh = new StaticFileHandler();

            sfh.ProcessRequest(context);

            return(new DefaultHandlerAsyncResult(callback, state));
        }
All Usage Examples Of System.Web.StaticFileHandler::ProcessRequest