本文整理汇总了C#中IHttpRequest.HasNotModifiedSince方法的典型用法代码示例。如果您正苦于以下问题:C# IHttpRequest.HasNotModifiedSince方法的具体用法?C# IHttpRequest.HasNotModifiedSince怎么用?C# IHttpRequest.HasNotModifiedSince使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IHttpRequest
的用法示例。
在下文中一共展示了IHttpRequest.HasNotModifiedSince方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessRequest
public void ProcessRequest(IHttpRequest request, IHttpResponse response, string operationName)
{
response.EndHttpRequest(skipClose: true, afterBody: r =>
{
var requestedPath = request.GetPhysicalPath();
var fi = new FileInfo(requestedPath);
foreach (var defaultDoc in EndpointHost.Config.DefaultDocuments)
{
var defaultFileName = Path.Combine(fi.FullName, defaultDoc);
if (!File.Exists(defaultFileName)) continue;
TimeSpan maxAge;
if (r.ContentType != null && EndpointHost.Config.AddMaxAgeForStaticMimeTypes.TryGetValue(r.ContentType, out maxAge))
{
r.AddHeader(HttpHeaders.CacheControl, "max-age=" + maxAge.TotalSeconds);
}
if (request.HasNotModifiedSince(fi.LastWriteTime))
{
r.ContentType = MimeTypes.GetMimeType(defaultFileName);
r.StatusCode = 304;
return;
}
try
{
r.AddHeaderLastModified(fi.LastWriteTime);
r.ContentType = MimeTypes.GetMimeType(defaultFileName);
if (defaultFileName.EqualsIgnoreCase(this.DefaultFilePath))
{
if (fi.LastWriteTime > this.DefaultFileModified)
SetDefaultFile(this.DefaultFilePath); //reload
r.OutputStream.Write(this.DefaultFileContents, 0, this.DefaultFileContents.Length);
r.Close();
return;
}
if (!Env.IsMono)
{
r.TransmitFile(defaultFileName);
}
else
{
r.WriteFile(defaultFileName);
}
}
catch (Exception ex)
{
log.ErrorFormat("Static file {0} forbidden: {1}", request.PathInfo, ex.Message);
throw new HttpException(403, "Forbidden.");
}
return;
}
});
}
示例2: ProcessRequest
public void ProcessRequest(IHttpRequest request, IHttpResponse response, string operationName)
{
response.EndHttpHandlerRequest (skipClose: true, afterBody: r => {
// i.e. /static/folder/file.html => folder/file.html
var requested_relative_path = request.GetAbsolutePath().Substring(RoutePath.Length);
if (requested_relative_path.Count (c => c == '.') == 0) {
// append index.html
if (requested_relative_path.EndsWith ("/")) {
requested_relative_path += "index.html";
} else
requested_relative_path += "/index.html";
}
var file_content = ReadInEmbeddedResource (requested_relative_path);
if (file_content == null)
throw new HttpException(404, "Not found");
r.ContentType = MimeTypes.GetMimeType(requested_relative_path);
TimeSpan maxAge;
if (r.ContentType != null && EndpointHost.Config.AddMaxAgeForStaticMimeTypes.TryGetValue(r.ContentType, out maxAge))
{
r.AddHeader(HttpHeaders.CacheControl, "max-age=" + maxAge.TotalSeconds);
}
if (request.HasNotModifiedSince(lastModified))
{
r.StatusCode = 304;
return;
}
try
{
r.AddHeaderLastModified(lastModified);
r.ContentType = MimeTypes.GetMimeType(requested_relative_path);
r.Write(file_content);
}
catch (Exception ex)
{
//log.ErrorFormat("Static file {0} forbidden: {1}", request.PathInfo, ex.Message);
throw new HttpException(400, "Server error.");
}
});
}
示例3: ProcessRequest
public void ProcessRequest(IHttpRequest request, IHttpResponse response, string operationName)
{
var fileName = request.GetPhysicalPath();
var fi = new FileInfo(fileName);
if (!fi.Exists)
{
if ((fi.Attributes & FileAttributes.Directory) != 0)
{
foreach (var defaultDoc in EndpointHost.Config.DefaultDocuments)
{
var defaultFileName = Path.Combine(fi.FullName, defaultDoc);
var defaultFileInfo = new FileInfo(defaultFileName);
if (!defaultFileInfo.Exists) continue;
response.Redirect(request.GetPathUrl() + '/' + defaultDoc);
return;
}
}
if (!fi.Exists)
throw new HttpException(404, "File '" + request.PathInfo + "' not found.");
}
if (request.HasNotModifiedSince(fi.LastWriteTime))
{
response.ContentType = MimeTypes.GetMimeType(fileName);
response.StatusCode = 304;
return;
}
try
{
response.AddHeaderLastModified(fi.LastWriteTime);
response.ContentType = MimeTypes.GetMimeType(fileName);
if (!Env.IsMono)
{
response.TransmitFile(fileName);
}
else
{
response.WriteFile(fileName);
}
}
catch (Exception)
{
throw new HttpException(403, "Forbidden.");
}
}
示例4: ProcessRequest
public void ProcessRequest(IHttpRequest request, IHttpResponse response, string operationName)
{
response.EndHttpRequest(skipClose: true, afterBody: r => {
var fileName = request.GetPhysicalPath();
var fi = new FileInfo(fileName);
if (!fi.Exists)
{
if ((fi.Attributes & FileAttributes.Directory) != 0)
{
foreach (var defaultDoc in EndpointHost.Config.DefaultDocuments)
{
var defaultFileName = Path.Combine(fi.FullName, defaultDoc);
if (!File.Exists(defaultFileName)) continue;
r.Redirect(request.GetPathUrl() + '/' + defaultDoc);
return;
}
}
if (!fi.Exists)
{
var originalFileName = fileName;
if (Env.IsMono)
{
//Create a case-insensitive file index of all host files
if (allFiles == null)
allFiles = CreateFileIndex(request.ApplicationFilePath);
if (allDirs == null)
allDirs = CreateDirIndex(request.ApplicationFilePath);
if (allFiles.TryGetValue(fileName.ToLower(), out fileName))
{
fi = new FileInfo(fileName);
}
}
if (!fi.Exists)
{
var msg = "Static File '" + request.PathInfo + "' not found.";
log.WarnFormat("{0} in path: {1}", msg, originalFileName);
throw new HttpException(404, msg);
}
}
}
TimeSpan maxAge;
if (r.ContentType != null && EndpointHost.Config.AddMaxAgeForStaticMimeTypes.TryGetValue(r.ContentType, out maxAge))
{
r.AddHeader(HttpHeaders.CacheControl, "max-age=" + maxAge.TotalSeconds);
}
if (request.HasNotModifiedSince(fi.LastWriteTime))
{
r.ContentType = MimeTypes.GetMimeType(fileName);
r.StatusCode = 304;
return;
}
try
{
r.AddHeaderLastModified(fi.LastWriteTime);
r.ContentType = MimeTypes.GetMimeType(fileName);
if (fileName.EqualsIgnoreCase(this.DefaultFilePath))
{
if (fi.LastWriteTime > this.DefaultFileModified)
SetDefaultFile(this.DefaultFilePath); //reload
r.OutputStream.Write(this.DefaultFileContents, 0, this.DefaultFileContents.Length);
r.Close();
return;
}
if (!Env.IsMono)
{
r.TransmitFile(fileName);
}
else
{
r.WriteFile(fileName);
}
}
catch (Exception ex)
{
log.ErrorFormat("Static file {0} forbidden: {1}", request.PathInfo, ex.Message);
throw new HttpException(403, "Forbidden.");
}
});
}
示例5: ProcessRequest
public void ProcessRequest(IHttpRequest request, IHttpResponse response, string operationName)
{
response.EndHttpHandlerRequest(skipClose: true, afterBody: r => {
var fileName = request.GetPhysicalPath();
var fi = new FileInfo(fileName);
if (!fi.Exists)
{
if ((fi.Attributes & FileAttributes.Directory) != 0)
{
foreach (var defaultDoc in EndpointHost.Config.DefaultDocuments)
{
var defaultFileName = Path.Combine(fi.FullName, defaultDoc);
if (!File.Exists(defaultFileName)) continue;
r.Redirect(request.GetPathUrl() + '/' + defaultDoc);
return;
}
}
if (!fi.Exists)
{
var originalFileName = fileName;
if (Env.IsMono)
{
//Create a case-insensitive file index of all host files
if (allFiles == null)
allFiles = CreateFileIndex(request.ApplicationFilePath);
if (allDirs == null)
allDirs = CreateDirIndex(request.ApplicationFilePath);
if (allFiles.TryGetValue(fileName.ToLower(), out fileName))
{
fi = new FileInfo(fileName);
}
}
if (!fi.Exists)
{
var msg = "Static File '" + request.PathInfo + "' not found.";
log.WarnFormat("{0} in path: {1}", msg, originalFileName);
throw new HttpException(404, msg);
}
}
}
TimeSpan maxAge;
if (r.ContentType != null && EndpointHost.Config.AddMaxAgeForStaticMimeTypes.TryGetValue(r.ContentType, out maxAge))
{
r.AddHeader(HttpHeaders.CacheControl, "max-age=" + maxAge.TotalSeconds);
}
if (request.HasNotModifiedSince(fi.LastWriteTime))
{
r.ContentType = MimeTypes.GetMimeType(fileName);
r.StatusCode = 304;
return;
}
try
{
r.AddHeaderLastModified(fi.LastWriteTime);
r.ContentType = MimeTypes.GetMimeType(fileName);
if (fileName.EqualsIgnoreCase(this.DefaultFilePath))
{
if (fi.LastWriteTime > this.DefaultFileModified)
SetDefaultFile(this.DefaultFilePath); //reload
r.OutputStream.Write(this.DefaultFileContents, 0, this.DefaultFileContents.Length);
r.Close();
return;
}
if (EndpointHost.Config.AllowPartialResponses)
r.AddHeader(HttpHeaders.AcceptRanges, "bytes");
long contentLength = fi.Length;
long rangeStart, rangeEnd;
var rangeHeader = request.Headers[HttpHeaders.Range];
if (EndpointHost.Config.AllowPartialResponses && rangeHeader != null)
{
rangeHeader.ExtractHttpRanges(contentLength, out rangeStart, out rangeEnd);
if (rangeEnd > contentLength - 1)
rangeEnd = contentLength - 1;
r.AddHttpRangeResponseHeaders(rangeStart: rangeStart, rangeEnd: rangeEnd, contentLength: contentLength);
}
else
{
rangeStart = 0;
rangeEnd = contentLength - 1;
r.SetContentLength(contentLength); //throws with ASP.NET webdev server non-IIS pipelined mode
}
var outputStream = r.OutputStream;
using (var fs = fi.OpenRead())
{
if (rangeStart != 0 || rangeEnd != fi.Length - 1)
{
fs.WritePartialTo(outputStream, rangeStart, rangeEnd);
//.........这里部分代码省略.........
示例6: ProcessRequest
public void ProcessRequest(IHttpRequest request, IHttpResponse response, string operationName)
{
response.EndHttpRequest(skipClose: true, afterBody: r => {
var fileName = request.GetPhysicalPath();
var fi = new FileInfo(fileName);
if (!fi.Exists)
{
if ((fi.Attributes & FileAttributes.Directory) != 0)
{
foreach (var defaultDoc in EndpointHost.Config.DefaultDocuments)
{
var defaultFileName = Path.Combine(fi.FullName, defaultDoc);
if (!File.Exists(defaultFileName)) continue;
r.Redirect(request.GetPathUrl() + '/' + defaultDoc);
return;
}
}
if (!fi.Exists)
throw new HttpException(404, "File '" + request.PathInfo + "' not found.");
}
TimeSpan maxAge;
if (r.ContentType != null && EndpointHost.Config.AddMaxAgeForStaticMimeTypes.TryGetValue(r.ContentType, out maxAge))
{
r.AddHeader(HttpHeaders.CacheControl, "max-age=" + maxAge.TotalSeconds);
}
if (request.HasNotModifiedSince(fi.LastWriteTime))
{
r.ContentType = MimeTypes.GetMimeType(fileName);
r.StatusCode = 304;
return;
}
try
{
r.AddHeaderLastModified(fi.LastWriteTime);
r.ContentType = MimeTypes.GetMimeType(fileName);
if (fileName.EqualsIgnoreCase(this.DefaultFilePath))
{
if (fi.LastWriteTime > this.DefaultFileModified)
SetDefaultFile(this.DefaultFilePath); //reload
r.OutputStream.Write(this.DefaultFileContents, 0, this.DefaultFileContents.Length);
r.Close();
return;
}
if (!Env.IsMono)
{
r.TransmitFile(fileName);
}
else
{
r.WriteFile(fileName);
}
}
catch (Exception)
{
throw new HttpException(403, "Forbidden.");
}
});
}
示例7: ProcessRequest
public void ProcessRequest(IHttpRequest request, IHttpResponse response, string operationName)
{
response.EndHttpHandlerRequest(skipClose: true, afterBody: r => {
// i.e. /static/folder/file.html => folder/file.html
var abs_path = request.GetAbsolutePath();
string requested_relative_path = "";
if (abs_path == "/")
requested_relative_path = "./";
else
requested_relative_path = abs_path.Substring(RoutePath.Length);
var fileName = Path.Combine (HtdocsPath, requested_relative_path);
var fi = new FileInfo(fileName);
if (!fi.Exists)
{
// append default filename if feasible (i.e. index.html)
if ((fi.Attributes & FileAttributes.Directory) != 0)
{
foreach (var defaultDoc in EndpointHost.Config.DefaultDocuments)
{
var defaultFileName = Path.Combine(fi.FullName, defaultDoc);
if (!File.Exists(defaultFileName)) continue;
fi = new FileInfo (defaultFileName);
fileName = defaultFileName;
}
}
if (!fi.Exists) {
var msg = "Static File '" + request.PathInfo + "' not found.";
throw new HttpException(404, msg);
}
}
r.ContentType = MimeTypes.GetMimeType(fileName);
TimeSpan maxAge;
if (r.ContentType != null && EndpointHost.Config.AddMaxAgeForStaticMimeTypes.TryGetValue(r.ContentType, out maxAge))
{
r.AddHeader(HttpHeaders.CacheControl, "max-age=" + maxAge.TotalSeconds);
}
if (request.HasNotModifiedSince(fi.LastWriteTime))
{
r.ContentType = MimeTypes.GetMimeType(fileName);
r.StatusCode = 304;
return;
}
try
{
r.AddHeaderLastModified(fi.LastWriteTime);
r.ContentType = MimeTypes.GetMimeType(fileName);
if (!Env.IsMono)
{
r.TransmitFile(fileName);
}
else
{
r.WriteFile(fileName);
}
}
catch (Exception ex)
{
//log.ErrorFormat("Static file {0} forbidden: {1}", request.PathInfo, ex.Message);
throw new HttpException(403, "Forbidden.");
}
});
}