Static files in Asp.Net Core

Original: Asp.Net Core Static file

Static files in Asp.Net Core

In this section we will discuss how to make the ASP.NET Core application that supports static files such as HTML, images, CSS, and JavaScript files.

Static files

  • By default, Asp.Net Core application does not serve static files.
  • The default directory is static files wwwroot, this directory must be located in the root directory of the project folder.

Copy and paste the wwwroot folder of images. We assume that the file name is banner.jpg. To be able to access this file from the browser, the path is: http://{{serverName}}/banner.jpgwe run on the local computer in our example, therefore URL will look like this. Port number on your computer may be different. http://localhost:3290/banner.jpg.

From My Computer, and then navigate to the top Url, we are still processed by Run()the middleware method returns the result response. I do not see the picture banner.jpg. This is because, at present our application request processing pipeline, can not provide the required middleware static files. We need to use middleware UseStaticFiles().

Modify Configure()code in the method, will be UseStaticFiles()added to the intermediate request processing pipeline in our application, as shown below.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    //添加静态文件中间件
    app.UseStaticFiles();

    app.Run(async (context) =>
    {
        await context.Response.WriteAsync("Hello World!");
    });
}

In wwwrootnot like the default template vs provide the same images, CSS, and JavaScript files folder classification, we recommend the different file types to distinguish between folders, refer to the diagram folder hierarchy:

12 1

In order to be able to access from the browser image1.jpgwe enter an address to http://localhost:49119/images/image1.jpgget results.

Provide static files outside the folder wwwroot file

By default, the UseStaticFiles()middleware provides only a static file wwwroot folder. If you prefer, we can also in wwwrootproviding static files outside the folder.

Provide default document

Most programs have a default Web document, the contents of the document it is displayed when users access the program address. For example, you have a named default.htmlfile, and you want to provide it when users access the application root URL, that is,http://localhost:3290

At this point, we have to look at access to this address, I see I'm using Run()the callback method to register middleware generated. But I do not see the default document default.htmlcontent. In order to provide the default page, we must be inserted in the request processing pipeline application UseDefaultFiles()中间件.

//添加默认文件中间件
app.UseDefaultFiles();
//添加静态文件中间件
app.UseStaticFiles();

Note: You must UseStaticFilesbefore registering UseDefaultFilesto provide a default file. UseDefaultFilesIs a URL rewriter, does not actually provide documentation. It just will URLoverride the default to locate the document, then static files or provided by the middleware. URL address bar is still the root URL, instead of rewriting the URL.

The following is a UseDefaultFilesmiddleware default will look for address information

- index.htm 的默认文件
- index.html
- default.htm
- default.html

If you want to use other documents, such as 52abp.html for example, as your default document, you can do this using the following code.

//将52abp.html指定为默认文档
DefaultFilesOptions defaultFilesOptions = new DefaultFilesOptions();
defaultFilesOptions.DefaultFileNames.Clear();
defaultFilesOptions.DefaultFileNames.Add(“52abp.html”);
//添加默认文件中间件
app.UseDefaultFiles(defaultFilesOptions);
//添加静态文件中间件
app.UseStaticFiles();

UseFileServer Middleware

UseFileServerIt combines UseStaticFiles,UseDefaultFiles和UseDirectoryBrowsermiddleware functions. DirectoryBrowser middleware, support for directory browsing, and allows users to view files in the specified directory. We can use UseFileServer middleware replacement UseStaticFiles and UseDefaultFiles middleware.

/使用UseFileServer而不是UseDefaultFiles和UseStaticFiles
FileServerOptions fileServerOptions = new FileServerOptions();
fileServerOptions.DefaultFilesOptions.DefaultFileNames.Clear();
fileServerOptions.DefaultFilesOptions.DefaultFileNames.Add(“52abp.html”);
app.UseFileServer(fileServerOptions);

Knowledge point to note here: we should use add middleware, is added to a request processing pipeline applications. In most cases, we use a method that begins to expand USE added middleware. E.g:

UseDeveloperExceptionPage()
UseDefaultFiles()
UseStaticFiles()
UseFileServer()

If you want to customize these middleware components, he may have a corresponding Configuration tab. Refer to the table: | Middleware | options object | | ------------------------- | ------------ ----------------- | | UseDeveloperExceptionPage | DeveloperExceptionPageOptions | | UseDefaultFiles | DefaultFilesOptions | | UseStaticFiles | StaticFileOptions | | UseFileServer | FileServerOptions |

 

Welcome to add a personal Micro Signal: Like if thoughts.

I welcome the attention of the public numbers, not only recommend the latest blog for you, there are more surprises waiting for you and resources! Learn together and common progress!

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11456470.html