aspnetcore/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserExtensions.cs

69 lines
2.3 KiB
C#

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.StaticFiles;
namespace Microsoft.AspNet.Builder
{
/// <summary>
/// Extension methods for the DirectoryBrowserMiddleware
/// </summary>
public static class DirectoryBrowserExtensions
{
/// <summary>
/// Enable directory browsing on the current path
/// </summary>
/// <param name="app"></param>
/// <returns></returns>
public static IApplicationBuilder UseDirectoryBrowser(this IApplicationBuilder app)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseDirectoryBrowser(options => { });
}
/// <summary>
/// Enables directory browsing for the given request path
/// </summary>
/// <param name="app"></param>
/// <param name="requestPath">The relative request path.</param>
/// <returns></returns>
public static IApplicationBuilder UseDirectoryBrowser(this IApplicationBuilder app, string requestPath)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseDirectoryBrowser(options => { options.RequestPath = new PathString(requestPath); });
}
/// <summary>
/// Enable directory browsing with the given options
/// </summary>
/// <param name="app"></param>
/// <param name="configureOptions"></param>
/// <returns></returns>
public static IApplicationBuilder UseDirectoryBrowser(this IApplicationBuilder app, Action<DirectoryBrowserOptions> configureOptions)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new DirectoryBrowserOptions();
configureOptions(options);
return app.UseMiddleware<DirectoryBrowserMiddleware>(options);
}
}
}