导航

03-006 StaticFiles 之 StaticFileMiddleware

Posted on 2015-03-13 10:07  DotNet1010  阅读(192)  评论(0)    收藏  举报

Invoke 方法部分代码:

                fileContext.ComprehendRequestHeaders();

                switch (fileContext.GetPreconditionState())
                {
		    #region -----switch
	            case StaticFileContext.PreconditionState.Unspecified:
                    case StaticFileContext.PreconditionState.ShouldProcess:
                        if (fileContext.IsHeadMethod)
                        {
							//Head Method 只返回状态 不返回内容
                            return fileContext.SendStatusAsync(Constants.Status200Ok);
                        }
                        if (fileContext.IsRangeRequest)
                        {
                            return fileContext.SendRangeAsync();
                        }
                        if (_logger.IsEnabled(LogLevel.Verbose))
                        {
                            _logger.WriteVerbose(string.Format("Copying file {0} to the response body", fileContext.SubPath));
                        }
                        return fileContext.SendAsync();

                    case StaticFileContext.PreconditionState.NotModified:
                        if (_logger.IsEnabled(LogLevel.Verbose))
                        {
                            _logger.WriteVerbose(string.Format("{0} not modified", fileContext.SubPath));
                        }
                        return fileContext.SendStatusAsync(Constants.Status304NotModified);

                    case StaticFileContext.PreconditionState.PreconditionFailed:
                        return fileContext.SendStatusAsync(Constants.Status412PreconditionFailed);

                    default:
                        var exception = new NotImplementedException(fileContext.GetPreconditionState().ToString());
                        _logger.WriteError("No precondition state specified", exception);
                        throw exception;
						#endregion 
				}

 上述代码:Head 请求的只返回状态码。

               Get请求返回部分    //Status206PartialContent  或者  Status416RangeNotSatisfiable

                或者文件的全部。  //Status 200

              PreconditionState.NotModified :则只返回状态代码:   304

              PreconditionState.PreconditionFailed:返回 状态代码 412