Learn more about using ASP. NET core static files tutorial

Source: Internet
Author: User
Tags throw exception
This article mainly for you to introduce the ASP. NET core static file use tutorial, with a certain reference value, interested in small partners can refer to

In this chapter, we will learn how to use files. Almost every Web application requires an important feature: the ability to provide files (static files) from the file system.

    • Static files like JavaScript files, images, CSS files, and so on, our ASP. NET core application can be directly provided to customers.

    • Static files are typically located in the Web root (wwwroot) folder.

    • By default, this is the only place where we can provide files directly from the file system.

Case

Now let's take a simple example to understand how we provide these static files in our application.

Here, we want to add a simple HTML file to our Firstappdemo application, which is placed in the Web root (wwwroot) folder. In Solution Explorer, right-click the Wwwroot folder and select add→ New Item.

In the middle pane, select the HTML page and call index.html, and click the Add button.

You will see a simple index.html file. Let's add some simple text and headings in it as shown below.


<! DOCTYPE html> 

When you run the application and enter index.html in the browser, you will see the app. The run middleware throws an exception because there is nothing in our application at the moment.

Now there is no middleware in our project to find any files on the file system.

To resolve this issue, go to NuGet Package Manager by right-clicking your project in Solution Explorer and selecting Manage NuGet package.

Searching for Microsoft.AspNet.StaticFiles will find the static file middleware. Let's install this NuGet package and now we can register the middleware in the Configure method.

Let's add the Usestaticfiles middleware in the Configure method shown in the following program.


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 what 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 the context.   Response.writeasync (msg);  });  }//Entry point for the application.  public static void Main (string[] args) = webapplication.run<startup> (args); } }

Unless you override the option by passing in some different configuration parameters, the static file is considered a request path for a given request. This request path is relative to the file system.

    • If a static file finds a file based on a URL, it returns the file directly without invoking the next block middleware.

    • If no matching file is found, it will continue to execute the next block middleware.

Let's save the Startup.cs file and refresh the browser.

You can now see the index.html file. Any javascript files, CSS files, or HTML files that you place anywhere under the Wwwroot folder can be used directly as static files in ASP.

    • IIS always has this feature if you want index.html to be your default file.

    • You can give IIS a default file list. If someone accesses the root directory, in this case, if IIS finds a file named index.html, it will automatically return the file to the client.

    • Let's start with a few changes now. First, we need to remove the mandatory error and then add another piece of middleware, which is usedefaultfiles. The following is an implementation of the configuration method.


/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 the context. Response.writeasync (msg);  }); }

This middleware will listen for incoming requests, and if the request is the root directory, see if there is a matching default file.

You can override this middleware option to tell it how to match the default file, but index.html is a default file by default.

Let's save the Startup.cs file and move your browser to the root of the Web application.

You can now see that index.html is the default file. The order in which you install the middleware is important, because if you place usedefaultfiles after usestaticfiles, you will probably not get the same results.

If you want to use Usedefaultfiles and Usestaticfiles middleware, you can use another middleware Microsoft.aspnet.staticfiles, which is also a nuget package, which is a server middleware. This essentially contains the default files and static files in the correct order.

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 the context. Response.writeasync (msg);  }); }

Let's save the Startup.cs file once again. Once you refresh the browser, you will see the same results as shown in the screenshot below.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.