在本章中,无涯教程将学习如何使用文件,几乎每个Web应用程序都需要的一个重要功能是能够从文件系统提供静态文件。
文件系统上的静态文件(如JavaScript文件,Img图像,CSS文件)是ASP.NET Core应用程序可以直接提供给客户端的资源。
静态文件通常位于Web根(wwwroot)文件夹中。
默认情况下,这是唯一可以直接从文件系统提供文件的地方。
现在,举一个简单的示例,在该示例中,将了解如何在应用程序中提供这些文件。
在这里,想向FirstAppDemo应用程序中添加一个简单的HTML文件,并且该HTML文件必须进入Web根目录(wwwroot)文件夹。在Solution Explorer中的wwwroot文件夹上单击鼠标右键,然后选择Add→New Item。
在中间窗格中,选择 HTML页面并将其命名为 index.html 并单击添加按钮。
您将看到一个简单的 index.html 文件。让无涯教程添加一些简单的文本和标题,如下所示。
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Welcome to ASP.NET Core</title> </head> <body> Hello, Wolrd! this message is from our first static HTML file. </body> </html>
当您运行应用程序并在浏览器中转到 index.html 时,您会看到 app.Run 中间件引发异常,因为应用程序中目前没有任何内容。
没有一块中间件会去寻找文件系统上要服务的任何文件。要解决此问题,请在 NuGet软件包管理器上右键单击Solution Explorer中的项目,然后选择"Manage NuGet Package"。
搜索 Microsoft.AspNet.StaticFiles ,它将找到Static Text middleware中间件,安装该nuget软件包,现在应该有其他方法可用于在Configure方法中注册中间件。
在Configure方法的中间添加 UseStaticFiles ,如以下程序所示。
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Configuration; namespace FirstAppDemo { public class Startup { public Startup() { var builder = new ConfigurationBuilder() .AddJsonFile("AppSettings.json"); Configuration = builder.Build(); } public IConfiguration Configuration { get; set; } //This method gets called by the runtime. //Use this method to add services to the container. //For more information on how to configure your application, //visit http://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { } //This method gets called by the runtime. //Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app) { app.UseIISPlatformHandler(); app.UseDeveloperExceptionPage(); app.UseRuntimeInfoPage(); app.UseStaticFiles(); app.Run(async (context) => { throw new System.Exception("Throw Exception"); var msg = Configuration["message"]; await context.Response.WriteAsync(msg); }); } //Entry point for the application. public static void Main(string[] args) => WebApplication.Run<Startup>(args); } }
除非您覆盖这些选项并传递一些不同的配置参数,然后将该请求路径与文件系统以及文件系统上的内容进行比较。
如果静态文件找到了可以使用的文件,它将为该文件提供服务,而不会调用下一个中间件。
如果找不到匹配的文件,则它将继续使用下一个中间件。
让无涯教程保存 Startup.cs 文件并刷新浏览器。
现在,您可以看到index.html文件。您在wwwroot内的任何位置放置的任何内容-任何JavaScript文件或CSS文件或HTML文件,都可以使用它们。
现在,如果您希望index.html成为默认文件,则这是IIS一直具有的功能。
您始终可以为IIS提供要查找的默认文件列表,如果有人进入目录的根目录,并且IIS找到名为index.html的文件,它将自动为该文件提供服务。
现在开始进行一些更改,首先,需要消除强制错误,然后添加另一个中间件,即UseDefaultFiles,以下是Configure方法的实现。
//This method gets called by the runtime. //Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app) { app.UseIISPlatformHandler(); app.UseDeveloperExceptionPage(); app.UseRuntimeInfoPage(); app.UseDefaultFiles(); app.UseStaticFiles(); app.Run(async (context) => { var msg = Configuration["message"]; await context.Response.WriteAsync(msg); }); }
这段中间件将查看传入的请求,并查看它是否用于目录的根目录以及是否有匹配的默认文件。
您可以覆盖此中间件的选项,以告诉它要查找的默认文件是什么,但是默认情况下,Index.html是默认文件之一。
保存 Startup.cs 文件,然后在浏览器中转到Web应用程序的根目录。
现在,您可以看到index.html是您的默认文件,安装中间件的顺序很重要,因为如果在UseStaticFiles之后使用UseDefaultFiles,则不会得到相同的输出。
如果要使用UseDefaultFiles和UseStaticFiles,则可能还需要另一个中间件,该中间件位于Microsoft.aspnet.staticfiles,NuGet包中,即 FileServer中间件,这实际上包括正确顺序的默认文件和静态文件。
//This method gets called by the runtime. //Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app) { app.UseIISPlatformHandler(); app.UseDeveloperExceptionPage(); app.UseRuntimeInfoPage(); app. UseFileServer(); app.Run(async (context) => { var msg = Configuration["message"]; await context.Response.WriteAsync(msg); }); }
再次保存 Startup.cs 文件。刷新浏览器后,您将看到与以下屏幕截图相同的输出。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)