asp.net core 使用 StaticFiles 中间件 (不完整翻译)
2016-08-02 14:28
666 查看
原文地址:https://docs.asp.net/en/latest/fundamentals/static-files.html
然后在
这样,我们就可以通过 http://<app>/MyImages 来浏览 wwwroot/images 文件夹。
注意
通过
default.htm
default.html
index.htm
index.html
从这个列表中第一个被找到的文件将会被像通过他的完整路径去访问一样的被展现出来(即使浏览器上显示的还是继续显示之前请求的 URII)
下面的代码展示了如何来改变默认显示的文件为 mydefault.html
原文后面的东西我没用上,所以就没有翻译了。。。
设置静态资源根目录
在Startup.cs中的
Configure方法中使用
StaticFiles中间件
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseStaticFiles(); // 使用默认的静态资源文件夹 /wwwroot // 使用指定的 /StaticFiles 文件夹 app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), @"MyStaticFiles")), RequestPath = new PathString("/StaticFiles") }); }
启用目录浏览功能
在Startup.Configure中添加下面的代码
app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\images")), RequestPath = new PathString("/MyImages") }); app.UseDirectoryBrowser(new DirectoryBrowserOptions() { FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\images")), RequestPath = new PathString("/MyImages") });
然后在
Startup.ConfigureServices中调用
AddDirectoryBrowser拓展方法
public void ConfigureServices(IServiceCollection services) { services.AddDirectoryBrowser(); }
这样,我们就可以通过 http://<app>/MyImages 来浏览 wwwroot/images 文件夹。
提供默认文档
设置一个默认的主页以便网站的访问者可以开始他们的浏览。为了能让你的网站在访客在不用提供完整页面路径的情况下能显示出默认页面,我们需要从Startup.Configure中调用
UseDefaultFiles拓展方法。
public void Configure(IApplicationBuilder app) { app.UseDefaultFiles(); app.UseStaticFiles(); }
注意
UseDefaultFile必须在
UseStaticFiles之前调用来提供默认页面。
UseDefaultFile只是一个 URL 重写组件,它并不会真正的发送文件到客户端,因此,我们必须启用 static files 中间件(
UseStaticFiles)来提供这个默认页面。
通过
UseDefaultFile,请求将会默认在文件夹中查找
default.htm
default.html
index.htm
index.html
从这个列表中第一个被找到的文件将会被像通过他的完整路径去访问一样的被展现出来(即使浏览器上显示的还是继续显示之前请求的 URII)
下面的代码展示了如何来改变默认显示的文件为 mydefault.html
public void Configure(IApplicationBuilder app) { // 设置网站默认提供浏览的文件 DefaultFilesOptions options = new DefaultFilesOptions(); options.DefaultFileNames.Clear(); options.DefaultFileNames.Add("mydefault.html"); app.UseDefaultFiles(options); app.UseStaticFiles(); }
原文后面的东西我没用上,所以就没有翻译了。。。
相关文章推荐
- ASP.NET Core 开发-中间件(StaticFiles)使用
- ASP.NET Core中使用GraphQL - 第二章 中间件
- 在ASP.NET Core Identity外面使用Cookie中间件
- [翻译] 如何在 ASP.Net Core 中使用 Consul 来存储配置
- Asp.net Core中如何使用中间件来管理websocket
- ASP.NET Core使用GraphQL第二章之中间件
- 学习ASP.NET Core, 怎能不了解请求处理管道[5]: 中间件注册可以除了可以使用Startup之外,还可以选择StartupFilter
- ASP.NET Core 中间件Diagnostics使用
- 在ASP.NET Core 中使用Cookie中间件
- 详解在ASP.NET Core 中使用Cookie中间件
- ASP.NET Core 使用 URL Rewrite 中间件实现 HTTP 重定向到 HTTPS
- ASP.NET Core 中间件Diagnostics使用
- ASP.NET Core -中间件(Middleware)使用
- (翻译)怎么在ASP.NET 2.0中使用Membership
- FCKeditor 在ASP.Net 中的使用说明(翻译)
- [翻译]在asp.net 2.0中结合母板页使用meta标签(扩展@Page指令)
- [翻译] Scott Mitchell 的ASP.NET 2.0数据操作教程之七:使用DropDownList过滤的主/从报表
- [导入](翻译)怎么在ASP.NET 2.0中使用Membership
- 使用ASP.NET 2.0 Profile存储用户信息_2[翻译] Level 200