【.Net Core】处理静态文件
静态文件存储在项目的 Web 根目录中。 默认目录是 <content_root>/wwwroot,但可通过 UseWebRoot 方法更改目录。
public class Program
{
public static void Main(string[] args)
{
//初始化配置
GlobalConfig.Init();
new MicroInitializer()
.Startup(
new ServiceConfig { Assembly = typeof(TenanteServicePlugin).Assembly,IsRemote=true,ServerAddress= GlobalConfig.Settings["TenantUrl"] },
new ServiceConfig { Assembly = typeof(EInvoiceServicPlugin).Assembly, IsRemote = true, ServerAddress = GlobalConfig.Settings["InvoiceUrl"] }
, new ServiceConfig { Assembly = typeof(CommonServiePlugin).Assembly }
);
BuildWebHost(args).Run();
} public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args).UseUrls("http://*:80")
.UseStartup<Startup>()
.Build();
}
WebHost.CreateDefaultBuilder(args)方法可将内容根目录设置为当前目录。
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
//提供默认文档
app.UseDefaultFiles();
app.UseStaticFiles(); app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
//调用提供 wwwroot 文件夹中的静态文件
app.UseStaticFiles(new StaticFileOptions
{
//设置 HTTP 响应标头
OnPrepareResponse = ctx =>
{
// Requires the following import:
// using Microsoft.AspNetCore.Http;
ctx.Context.Response.Headers.Append("key", "saas.server.web");
ctx.Context.Response.Headers.Append("token", "saas.inner");
},
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "config")),
RequestPath = "/json"
}); //启用目录浏览
app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "config")),
RequestPath = "/json"
}); }
}
app.UseStaticFiles()方法重载将 Web 根目录中的文件标记为可用。
app.UseDefaultFiles():提供默认文档
【.Net Core】处理静态文件的更多相关文章
- 基于 Vue.js 之 iView UI 框架非工程化实践记要 使用 Newtonsoft.Json 操作 JSON 字符串 基于.net core实现项目自动编译、并生成nuget包 webpack + vue 在dev和production模式下的小小区别 这样入门asp.net core 之 静态文件 这样入门asp.net core,如何
基于 Vue.js 之 iView UI 框架非工程化实践记要 像我们平日里做惯了 Java 或者 .NET 这种后端程序员,对于前端的认识还常常停留在 jQuery 时代,包括其插件在需要时就引 ...
- C#编译器优化那点事 c# 如果一个对象的值为null,那么它调用扩展方法时为甚么不报错 webAPI 控制器(Controller)太多怎么办? .NET MVC项目设置包含Areas中的页面为默认启动页 (五)Net Core使用静态文件 学习ASP.NET Core Razor 编程系列八——并发处理
C#编译器优化那点事 使用C#编写程序,给最终用户的程序,是需要使用release配置的,而release配置和debug配置,有一个关键区别,就是release的编译器优化默认是启用的.优化代码 ...
- ASP.NET Core使用静态文件、目录游览与MIME类型管理
前言 今天我们来了解了解ASP.NET Core中的静态文件的处理方式. 以前我们寄宿在IIS中的时候,很多静态文件的过滤 和相关的安全措施 都已经帮我们处理好了. ASP.NET Core则不同,因 ...
- 这样入门asp.net core 之 静态文件
本文章主要说明asp.net core中静态资源处理方案: 一.静态文件服务 首先明确contentRoot和webroot这两个概念 contentRoot:web的项目文件夹,其中包含webroo ...
- 让.net core 支持静态文件
想不到默认的.net core竟然不支持静态文件,还需要额外配置中间件来支持 1.Nuget安装 Microsoft.aspnetcore.staticfiles 2.在Startup.cs中使用服 ...
- asp.net core 控制静态文件的授权
静态文件访问在网站中是一项重要的服务,用于向前端提供可以直接访问的文件,如js,css,文档等,方法是在Startup的Configure中添加UseStaticFiles()管道. 参考:ASP.N ...
- .NET Core MVC 静态文件应用
一.静态文件应用方面 ASP.NET Core 静态文件应用,主要分为两方面:网站访问和静态文件整合 二.案例 1.访问静态文件 我们都知道,在 ASP.NET 项目中,我们的静态文件一般要放在 ww ...
- 利用ASP .NET Core的静态文件原理实现远程访问Nlog日志内容及解决遇到的坑
最近项目上试运行发现,很多时候网站出了问题或者某个功能不正常,常常需要运维人员去服务器里面查看一下日志,看看日志里面会产生什么异常,这样导致每次都要去远程服务器很不方便,有时服务器是客户保管的不能让我 ...
- 细说ASP.NET Core静态文件的缓存方式
一.前言 我们在优化Web服务的时候,对于静态的资源文件,通常都是通过客户端缓存.服务器缓存.CDN缓存,这三种方式来缓解客户端对于Web服务器的连接请求压力的. 本文指在这三个方面,在ASP.NET ...
- NET Core静态文件的缓存方式
NET Core静态文件的缓存方式 阅读目录 一.前言 二.StaticFileMiddleware 三.ASP.NET Core与CDN? 四.写在最后 回到目录 一.前言 我们在优化Web服务的时 ...
随机推荐
- laravel 邮件配置
.env的配置 MAIL_DRIVER=smtpMAIL_HOST=smtp.163.comMAIL_PORT=465MAIL_USERNAME=你的163邮箱地址MAIL_PASSWORD=你的16 ...
- unittest中的Empty suite错误
import unittest from selenium import webdriver class ibdata(unittest.TestCase): @classmethod def set ...
- python获取函数注释 __doc__
使用 help 函数 可以查看 函数的注释内容 但是它也有点"添油加醋" 其实函数的注释被保存在 __doc__属性里面 PS 双下划线 def f(): "&quo ...
- week01-绪论
一.作业题目 仿照三元组或复数的抽象数据类型写出有理数抽象数据类型的描述 (有理数是其分子.分母均为整数且分母不为零的分数). 有理数基本运算: 构造有理数T,元素e1,e2分别被 ...
- [Swift]LeetCode123. 买卖股票的最佳时机 III | Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [Swift]LeetCode214. 最短回文串 | Shortest Palindrome
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [Swift]LeetCode280. 摆动排序 $ Wiggle Sort
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- [Swift]LeetCode689. 三个无重叠子数组的最大和 | Maximum Sum of 3 Non-Overlapping Subarrays
In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...
- [Swift]LeetCode739. 每日温度 | Daily Temperatures
Given a list of daily temperatures T, return a list such that, for each day in the input, tells you ...
- [Swift]LeetCode911. 在线选举 | Online Election
In an election, the i-th vote was cast for persons[i] at time times[i]. Now, we would like to implem ...