导航

03-002 StaticFiles 之 DirectoryBrowserMiddleware

Posted on 2015-03-12 12:42  DotNet1010  阅读(153)  评论(0)    收藏  举报

Invoke 代码:

 public Task Invoke(HttpContext context)
        {
            // Check if the URL matches any expected paths
            PathString subpath;
            IDirectoryContents contents;
            if (Helpers.IsGetOrHeadMethod(context.Request.Method)
                && Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out subpath)
                && TryGetDirectoryInfo(subpath, out contents))
            {
                // If the path matches a directory but does not end in a slash, redirect to add the slash.
                // This prevents relative links from breaking.
                if (!Helpers.PathEndsInSlash(context.Request.Path))
                {
                    context.Response.StatusCode = 301;
                    context.Response.Headers[HeaderNames.Location] = context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString;
                    return Constants.CompletedTask;
                }

                return _options.Formatter.GenerateContentAsync(context, contents);
            }

            return _next(context);
        }

 请求必须是 Get 或者 Head

如果不是以 ”/" 结尾 则 返回 StatusCode=301.

如果是以“/" j结尾 则 生成内容 默认使用的是: HtmlDirectoryFormatter 生成Table表格显示内容。