前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >这样入门asp.net core 之 静态文件

这样入门asp.net core 之 静态文件

作者头像
sam dragon
发布2018-03-28 13:00:54
1.3K0
发布2018-03-28 13:00:54
举报
文章被收录于专栏:cnblogscnblogs

本文章主要说明asp.net core中静态资源处理方案:

一、静态文件服务

首先明确contentRoot和webroot这两个概念

  • contentRoot:web的项目文件夹,其中包含webroot和其他bin等其他文件夹
  • webroot:webroot是站点文件夹,可用url访问的文件夹。默认为:"contentroot/wwwroot"
  • 实现代码如下 Program中的代码
代码语言:javascript
复制
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
    .UseStartup<Startup>()
    .UseKestrel()
    .UseContentRoot(Directory.GetCurrentDirectory()) //设置contentroot
    .UseWebRoot("mywwwroot") //设置webroot
    .UseUrls("http://*:5000") //其他电脑可以用ip地址访问
    .Build();

     StartUp中的代码

代码语言:javascript
复制
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseStaticFiles();//开启静态文件访问
    //自定义静态文件访问
    app.UseStaticFiles(new StaticFileOptions(){
        FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "mystatic")),
        RequestPath = new PathString("/sam/static")
    });
     //配置mvc
    app.UseMvc(routers=>{
        routers.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
    });
}

效果图下图:

image
image

1.1 目录浏览

实现代码如下:

代码语言:javascript
复制
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{  
    app.UseDirectoryBrowser(new DirectoryBrowserOptions(){
        FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "Controllers")),
        RequestPath = new PathString("/controller")
    }); 
}
image
image

1.2 默认文档

app.UseDefaultFiles方法开启默认访问的配置,配置项用DefaultFilesOption类表示,代码如下:

代码语言:javascript
复制
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{  
    //默认文件
    DefaultFilesOptions defaultFiles = new DefaultFilesOptions();
    defaultFiles.DefaultFileNames.Clear();
    defaultFiles.DefaultFileNames.Add("myindex.html");
    app.UseDefaultFiles(defaultFiles); 
    
    app.UseStaticFiles(); //开启静态文件访问
}

注意此配置一定要在所有Use之前,否则设置不生效

1.3 UseFileServer

UserFileServer包含了UseStaticFiles, UseDefaultFiles, UserDirectoryBrowser的功能

代码语言:javascript
复制
app.UseFileServer(new FileServerOptions(){
    EnableDefaultFiles, //是否开启默认文档
    EnableDirectoryBrowsing, //是否开启目录浏览
    DefaultFilesOptions, //默认文件设置
    StaticFileOptions, //静态资源访问设置
    DirectoryBrowserOptions, //目录浏览设置
});

二、静态文件授权

静态模块是不对文件进行权限检查的,包含wwwroot下的文件和文件夹。如果相进行权限控制,可以使用action返回一个FileResult来实现:

代码语言:javascript
复制
private string basePath = Common.Uitls.HostingEnvironment.ContentRootPath;
public FileResult Index(int id){
    if(id == 1){
        return new PhysicalFileResult(Path.Combine(basePath, "aufolder","author.html"), "text/html");
    }
    return new PhysicalFileResult(Path.Combine(basePath, "error.html"), "text/html");;
}

三、FileExtensionContentTypeProvider类的使用

此类包含一个将文件扩展名映射到MIME内容类型的集合,代码如下:

代码语言:javascript
复制
FileExtensionContentTypeProvider provider=new FileExtensionContentTypeProvider();
provider.Mappings.Add(".sam", "text/plain");
//自定义静态文件访问
app.UseStaticFiles(new StaticFileOptions(){
    FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "mystatic")),
    RequestPath = new PathString("/sam/static"),
    ContentTypeProvider = provider
});
  • FileExtensionContentTypeProvider类与UseStaticFiles关联使用
  • 扩展名以 "." 开头。
  • 运行结果如下:
image
image
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-03-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、静态文件服务
    • 1.1 目录浏览
      • 1.2 默认文档
        • 1.3 UseFileServer
        • 二、静态文件授权
        • 三、FileExtensionContentTypeProvider类的使用
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档