📜 ⬆️ ⬇️

Preparing ASP.NET5, issue number 1 - let's talk about static content

With this article I open the author's column about technologies, secrets, tips and best practices of the new version of ASP.NET5. The .NET platform is rapidly moving towards cross-platform and its web part — ASP.NET — including. You have already heard and can find a lot of information on the network, including on Habre, that .NET is becoming an open source, about releasing new versions and official support of .NET on OSX, Linux. Together with these announcements and ASP.NET5 becomes available on more platforms for a larger circle of developers.


Within this column, my colleagues, invited experts, developers, and I will periodically talk about interesting things from the ASP.NET world, best practices, and share my experience using the platform in practice. At the end of each column, we will provide additional materials, links and cover news.

The theme of the first release - working with static files in ASP.NET5 - the task is as trivial as it is important.

aspnetcolumngithub Tip! You can try everything yourself or by downloading the source code from GitHub https://github.com/vyunev/aspnetcolumn1 .

New approach


In ASP.NET5, the approach to working with static files has changed a lot, in several ways at once. Let's start with the fact that the project in Visual Studio and when working with projects in other editors for static content is offered a separate folder - wwwroot (Fig. 1). In fact, the contents of wwwroot now displays a file representation of the site on a web server.
')
image
Fig.1 - The structure of the project ASP.NET5 (beta5)

Using a separate folder for static content allows you to simplify many tasks, including working with version control systems, caching content on CDNs, minifying tasks and packaging files, and configuring request processing by a web server.

In addition, the introduction of wwwroot allows you to make the site safer, since requests to files on the site can now be addressed only to the contents of wwwroot, and external configuration or other files outside this folder are not available to request.

If desired, developers can change the name of this folder using the webroot configuration parameter in project.json (Figure 2).

image
Fig.2 - Changing the folder name for static content through the configuration file

Tip! To rename a folder through Visual Studio, the best thing to do is to first rename it in Solution Explorer and only then change the configuration file. Otherwise, smart Visual Studio will create a new folder on its own.

Another important configuration parameter is the inclusion of the Microsoft.AspNet.StaticFiles package in the list of ASP.NET project dependencies, this package allows the platform pipeline to process static content (Figure 3).

image
Fig.3 - Inclusion of a package in the list of ASP.NET project dependencies

After the package is included in the dependency list, you need to add processing to the ASP.NET query processing pipeline. This configuration is performed in the Configure method of the special service class Startup , which by default is located in the Startup.cs file (Fig. 4).

image
Fig.4 - Enable processing of static files

After the specified configuration, all static content requested from the site will be given from a specific folder configuration.

Expansion of functionality


In ASP.NET5, every part of the platform can be redefined, extended or replaced. We can define our own handler of static files or supplement the existing one by specifying new rules. For example, if we want to add the ability to return * .json files as static, we only need to define the following content provider:

Listing 1. - Content type provider definition
image

Now we can use the new content type to expand the existing static file processing mechanism (Listing 2).

Listing 2. - Using a content type provider
image

When determining the operation of the request processing pipeline for static files, you can specify several more parameters. For this, StaticFileOptions has a set of properties. I will list some of them:


This can complete the introduction to the topic of processing static content based on ASP.NET5.

Latest news


Released ASP.NET5 Beta5 update with many changes, enhancements and bug fixes. Details of the update can be found in this blog . Carefully read the list of changes that may affect the previous code when updating.

With the release of ASP.NET5, Beta5 has updated the ASP.NET 5 toolkit yeoman generators , which supports cross-platform development on ASP.NET and allows you to generate code (scaffolding) for projects from the command line in any OS. About the project itself can be read here .

The image of the container Docker with ASP.NET5 Beta5 is published on the official Docker Hub.

Published reports of the DevCon 2015 conference, including on web development and the topic of ASP.NET .

useful links


The latest ASP.NET5 documentation is located at http://docs.asp.net/en/latest/ .

Check out the Damian Edwards PiDnx demo project, which shows how to run ASP.NET5 on Raspberry Pi 2 based on Windows 10 IoT Core.

Learn the basics of ASP.NET5 with the new free course of the Microsoft Virtual Academy.

To authors


Friends, if you are interested in supporting the column with your own material, please write to me at vyunev@microsoft.com to discuss all the details. We are looking for authors who can interestingly tell about ASP.NET and other topics.

about the author


Vladimir Yunev
Strategic Technologies Expert, Microsoft, Russia

Vladimir Yunev is a certified application developer with more than 12 years of experience. He works in the Department of Strategic Technologies at Microsoft Russia, responsible for topics related to the use of Microsoft cloud and web technologies in web applications, services and mobile clients. Permanent participant and organizer of many Russian IT conferences, the author of a book on the development of web applications on the ASP.NET MVC platform. In the community of developers known under the nickname XaocCPS.

I will be glad to talk with you: Twitter - https://twitter.com/XaocCPS and Facebook - https://www.facebook.com/yunev .

Source: https://habr.com/ru/post/261893/


All Articles