当前位置: 首页>>代码示例>>C#>>正文


C# IHttpRequest.TryGetIfLastModifiedSince方法代码示例

本文整理汇总了C#中IHttpRequest.TryGetIfLastModifiedSince方法的典型用法代码示例。如果您正苦于以下问题:C# IHttpRequest.TryGetIfLastModifiedSince方法的具体用法?C# IHttpRequest.TryGetIfLastModifiedSince怎么用?C# IHttpRequest.TryGetIfLastModifiedSince使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IHttpRequest的用法示例。


在下文中一共展示了IHttpRequest.TryGetIfLastModifiedSince方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Handle

        public HttpResponse Handle(IHttpRequest req)
        {
            var requestedFilename = HttpUtility.UrlDecode(req.Path.Substring(reqPathFilenameStartIndex));
            var absFilename = Path.GetFullPath(Path.Combine(directory, requestedFilename));
            if (!absFilename.StartsWith(directory))
            {
                return Forbidden;
            }

            var fileInfo = new FileInfo(absFilename);
            if (!fileInfo.Exists) // cached but deleted
            {
                return NotFound;
            }

            DateTime ifModifiedSince;
            CachedFile cachedFile;
            HttpResponse response;

            if (req.TryGetIfLastModifiedSince(out ifModifiedSince) && fileInfo.LastWriteTimeUtc <= ifModifiedSince)
            {
                // Remote client (browser) has a valid cached copy (we may not have it in cache yet though eg. server restarted)
                response = new HttpResponse(HttpStatusCode.NotModified, MimeMapping.GetMimeMapping(fileInfo.Name));
            }
            else if (cache.TryGetValue(absFilename, out cachedFile) && fileInfo.LastWriteTimeUtc <= cachedFile.LastModified)
            {
                // Dictionary cached copy is still valid
                response = req.IsAcceptGZip() ? cachedFile.GZippedResponse : cachedFile.Response;
            }
            else
            {
                // Load fresh from disk
                if (fileInfo.Length <= maxFileLengthToCache)
                {
                    var fileResponse = new FileResponse.Buffered(fileInfo);
                    var gzippedFileResponse = gzipFunc(fileResponse);
                    cache[absFilename] = new CachedFile(fileResponse, gzippedFileResponse);
                    response = req.IsAcceptGZip() ? gzippedFileResponse : fileResponse;
                }
                else
                {
                    response = new FileResponse.Stream(fileInfo);
                }
            }

            return response;
        }
开发者ID:joongonn,项目名称:mHttp,代码行数:47,代码来源:StaticFileHandler.cs

示例2: Handle

        public HttpResponse Handle(IHttpRequest req)
        {
            var fullName = GetFileFullName(req);
            var fileInfo = new FileInfo(fullName);

            if (!fileInfo.Exists) // cached but deleted
            {
                return NotFound;
            }

            DateTime ifModifiedSince;
            CachedFile cachedFile;

            var checkLastModified = req.TryGetIfLastModifiedSince(out ifModifiedSince); // remote client has a cached version
            var hasCachedResponse = checkLastModified ? TryGetCachedFileResponse(fullName, ifModifiedSince, out cachedFile) : TryGetCachedFileResponse(fullName, out cachedFile);

            if (hasCachedResponse && fileInfo.LastWriteTimeUtc <= cachedFile.LastModified)
            {
                if (checkLastModified)
                {
                    return NotChanged(cachedFile.ContentType);
                }
                else
                {
                    return req.IsAcceptGZip() && cachedFile.GZippedResponse != null ? cachedFile.GZippedResponse : cachedFile.Response;
                }
            }
            else
            {
                var fileResponse = new FileResponse(fileInfo);

                cachedFile = new CachedFile(fileResponse, gzipFunc != null ? fileResponse.GZip(gzipFunc) : null);
                cache[fullName] = cachedFile;

                return req.IsAcceptGZip() && cachedFile.GZippedResponse != null ? cachedFile.GZippedResponse : cachedFile.Response;
            }
        }
开发者ID:CubeCoders,项目名称:mHttp,代码行数:37,代码来源:StaticFileHandler.cs


注:本文中的IHttpRequest.TryGetIfLastModifiedSince方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。