本文整理汇总了C#中IHttpRequest.GetPhysicalPath方法的典型用法代码示例。如果您正苦于以下问题:C# IHttpRequest.GetPhysicalPath方法的具体用法?C# IHttpRequest.GetPhysicalPath怎么用?C# IHttpRequest.GetPhysicalPath使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IHttpRequest
的用法示例。
在下文中一共展示了IHttpRequest.GetPhysicalPath方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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)
{
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;
response.Redirect(request.GetPathUrl() + '/' + defaultDoc);
return;
}
}
if (!fi.Exists)
throw new HttpException(404, "File '" + request.PathInfo + "' not found.");
}
TimeSpan maxAge;
if (response.ContentType != null && EndpointHost.Config.AddMaxAgeForStaticMimeTypes.TryGetValue(response.ContentType, out maxAge))
{
response.AddHeader(HttpHeaders.CacheControl, "max-age=" + maxAge.TotalSeconds);
}
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.");
}
}
示例3: GetHandler
// Entry point for HttpListener
public static IHttpHandler GetHandler(IHttpRequest httpReq)
{
foreach (var rawHttpHandler in RawHttpHandlers)
{
var reqInfo = rawHttpHandler(httpReq);
if (reqInfo != null) return reqInfo;
}
var mode = EndpointHost.Config.ServiceStackHandlerFactoryPath;
var pathInfo = httpReq.PathInfo;
//Default Request /
if (string.IsNullOrEmpty(pathInfo) || pathInfo == "/")
{
if (ApplicationBaseUrl == null)
SetApplicationBaseUrl(httpReq.GetPathUrl());
//e.g. CatchAllHandler to Process Markdown files
var catchAllHandler = GetCatchAllHandlerIfAny(httpReq.HttpMethod, pathInfo, httpReq.GetPhysicalPath());
if (catchAllHandler != null) return catchAllHandler;
return ServeDefaultHandler ? DefaultHttpHandler : NonRootModeDefaultHttpHandler;
}
if (mode != null && pathInfo.EndsWith(mode))
{
var requestPath = pathInfo;
if (requestPath == "/" + mode
|| requestPath == mode
|| requestPath == mode + "/")
{
//TODO: write test for this
if (httpReq.GetPhysicalPath() != WebHostPhysicalPath
|| !File.Exists(Path.Combine(httpReq.ApplicationFilePath, DefaultRootFileName ?? "")))
{
return new IndexPageHttpHandler();
}
}
var okToServe = ShouldAllow(httpReq.GetPhysicalPath());
return okToServe ? DefaultHttpHandler : ForbiddenHttpHandler;
}
return GetHandlerForPathInfo(httpReq.HttpMethod, pathInfo, pathInfo, httpReq.GetPhysicalPath())
?? NotFoundHttpHandler;
}
示例4: ReturnDefaultHandler
private static IHttpHandler ReturnDefaultHandler(IHttpRequest httpReq)
{
var pathProvider = HostContext.VirtualFileSources;
var defaultDoc = pathProvider.GetFile(DefaultRootFileName ?? "");
if (httpReq.GetPhysicalPath() != WebHostPhysicalPath
|| defaultDoc == null)
{
return new IndexPageHttpHandler();
}
var okToServe = ShouldAllow(httpReq.GetPhysicalPath());
return okToServe ? DefaultHttpHandler : ForbiddenHttpHandler;
}
示例5: GetHandler
// Entry point for HttpListener
public static IHttpHandler GetHandler(IHttpRequest httpReq)
{
var appHost = HostContext.AppHost;
foreach (var rawHttpHandler in appHost.RawHttpHandlers)
{
var reqInfo = rawHttpHandler(httpReq);
if (reqInfo != null) return reqInfo;
}
var mode = appHost.Config.HandlerFactoryPath;
var pathInfo = httpReq.PathInfo;
//Default Request /
if (string.IsNullOrEmpty(pathInfo) || pathInfo == "/")
{
//If the fallback route can handle it, let it
if (appHost.Config.FallbackRestPath != null)
{
string contentType;
var sanitizedPath = RestHandler.GetSanitizedPathInfo(pathInfo, out contentType);
var restPath = appHost.Config.FallbackRestPath(httpReq.HttpMethod, sanitizedPath, httpReq.GetPhysicalPath());
if (restPath != null)
{
return new RestHandler { RestPath = restPath, RequestName = restPath.RequestType.GetOperationName(), ResponseContentType = contentType };
}
}
SetApplicationBaseUrl(httpReq.GetPathUrl());
//e.g. CatchAllHandler to Process Markdown files
var catchAllHandler = GetCatchAllHandlerIfAny(httpReq.HttpMethod, pathInfo, httpReq.GetPhysicalPath());
if (catchAllHandler != null) return catchAllHandler;
if (mode == null)
return DefaultHttpHandler;
if (DefaultRootFileName != null)
return StaticFilesHandler;
return NonRootModeDefaultHttpHandler;
}
if (mode != null && pathInfo.EndsWith("/" + mode))
{
return ReturnDefaultHandler(httpReq);
}
return GetHandlerForPathInfo(httpReq.HttpMethod, pathInfo, pathInfo, httpReq.GetPhysicalPath())
?? NotFoundHttpHandler;
}
示例6: GetHandler
// Entry point for HttpListener
public static IHttpHandler GetHandler(IHttpRequest httpReq)
{
var appHost = HostContext.AppHost;
foreach (var rawHttpHandler in appHost.RawHttpHandlers)
{
var reqInfo = rawHttpHandler(httpReq);
if (reqInfo != null) return reqInfo;
}
var mode = appHost.Config.ServiceStackHandlerFactoryPath;
var pathInfo = httpReq.PathInfo;
//Default Request /
if (string.IsNullOrEmpty(pathInfo) || pathInfo == "/")
{
if (ApplicationBaseUrl == null)
SetApplicationBaseUrl(httpReq.GetPathUrl());
//e.g. CatchAllHandler to Process Markdown files
var catchAllHandler = GetCatchAllHandlerIfAny(httpReq.HttpMethod, pathInfo, httpReq.GetPhysicalPath());
if (catchAllHandler != null) return catchAllHandler;
if (mode == null)
return DefaultHttpHandler;
if (DefaultRootFileName != null)
return StaticFileHandler;
return NonRootModeDefaultHttpHandler;
}
if (mode != null && pathInfo.EndsWith(mode))
{
var requestPath = pathInfo;
if (requestPath == "/" + mode
|| requestPath == mode
|| requestPath == mode + "/")
{
var pathProvider = HostContext.VirtualPathProvider;
var defaultDoc = pathProvider.GetFile(DefaultRootFileName ?? "");
if (httpReq.GetPhysicalPath() != WebHostPhysicalPath
|| defaultDoc == null)
{
return new IndexPageHttpHandler();
}
}
var okToServe = ShouldAllow(httpReq.GetPhysicalPath());
return okToServe ? DefaultHttpHandler : ForbiddenHttpHandler;
}
return GetHandlerForPathInfo(httpReq.HttpMethod, pathInfo, pathInfo, httpReq.GetPhysicalPath())
?? NotFoundHttpHandler;
}
示例7: 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.");
}
});
}
示例8: 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);
//.........这里部分代码省略.........
示例9: 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.");
}
var strHeader = request.Headers["If-Modified-Since"];
try
{
if (strHeader != null)
{
var dtIfModifiedSince = DateTime.ParseExact(strHeader, "r", null);
var ftime = fi.LastWriteTime.ToUniversalTime();
if (ftime <= dtIfModifiedSince)
{
response.ContentType = MimeTypes.GetMimeType(fileName);
response.StatusCode = 304;
return;
}
}
}
catch { }
try
{
var lastWT = fi.LastWriteTime.ToUniversalTime();
response.AddHeader("Last-Modified", lastWT.ToString("r"));
response.ContentType = MimeTypes.GetMimeType(fileName);
if (!Env.IsMono)
{
response.TransmitFile(fileName);
}
else
{
response.WriteFile(fileName);
}
}
catch (Exception)
{
throw new HttpException(403, "Forbidden.");
}
}