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


C# IHttpRequest.GetPhysicalPath方法代码示例

本文整理汇总了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;
                }
            });
        }
开发者ID:wiggle,项目名称:papercut-web,代码行数:56,代码来源:Program.cs

示例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.");
            }
        }
开发者ID:theouteredge,项目名称:ServiceStack,代码行数:54,代码来源:StaticFileHandler.cs

示例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;
        }
开发者ID:KennyBu,项目名称:ServiceStack,代码行数:47,代码来源:ServiceStackHttpHandlerFactory.cs

示例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;
        }
开发者ID:tchrikch,项目名称:ServiceStack,代码行数:14,代码来源:HttpHandlerFactory.cs

示例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;
        }
开发者ID:tchrikch,项目名称:ServiceStack,代码行数:53,代码来源:HttpHandlerFactory.cs

示例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;
        }
开发者ID:powareverb,项目名称:ServiceStack,代码行数:57,代码来源:HttpHandlerFactory.cs

示例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.");
                }
            });
        }
开发者ID:namman,项目名称:ServiceStack,代码行数:90,代码来源:StaticFileHandler.cs

示例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);
//.........这里部分代码省略.........
开发者ID:jmaucher,项目名称:SStack,代码行数:101,代码来源:StaticFileHandler.cs

示例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.");
            }
        }
开发者ID:Braunson,项目名称:ServiceStack,代码行数:61,代码来源:StaticFileHandler.cs


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