Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Static File processing of Nancy

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Let's first take a brief look at how these static files in our project are managed in the familiar ASP.NET MVC.

In fact, when we created a new MVC project, we already generated a "template" for our reference.

This "template" is the BundleConfig.cs under App_Start

1 public class BundleConfig 2 {3 / / For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862 4 public static void RegisterBundles (BundleCollection bundles) 5 {6 bundles.Add (new ScriptBundle ("~ / bundles/jquery"). Include (7 "~ / Scripts/jquery- {version} .js")) 8 bundles.Add (new ScriptBundle ("~ / bundles/jqueryval"). Include (9 "~ / Scripts/jquery.validate*"); 10 / / Use the development version of Modernizr to develop with and learn from. Then, when you're11 / / ready for production, use the build tool at http://modernizr.com to pick only the tests you need.12 bundles.Add (new ScriptBundle ("~ / bundles/modernizr"). Include (13 "~ / Scripts/modernizr-*")) 14 bundles.Add (new ScriptBundle ("~ / bundles/bootstrap"). Include (15 "~ / Scripts/bootstrap.js", 16 "~ / Scripts/respond.js") 17 bundles.Add (new StyleBundle ("~ / Content/css"). Include (18 "~ / Content/bootstrap.css", 19 "~ / Content/site.css"); 20} 21}

ScriptBundle and StyleBundle are the classes used to manage js and css respectively, both of which inherit the Bundle class!

It is located in the System.Web.Optimization assembly, if you want to use this feature, remember to add references!

So how do we use this?

Now suppose there are two folders, css and js, under the root directory, where Style1.css, Style2.css, js1.js and js2.js are stored, respectively.

Let's take a look at how to hand it over to Bundle management

1 bundles.Add (new ScriptBundle ("~ / bundles/js"). Include (2 "~ / js/js1.js", 3 "~ / js/js2.js"); 4 bundles.Add (new StyleBundle ("~ / bundles/css"). Include (5 "~ / css/Style1.css", 6 "~ / css/Style2.css"))

"~ / bundles/js" and "~ / bundles/css" are virtual paths!

Then it is used in the page (that is, the virtual path we just used)

1 @ Styles.Render ("~ / bundles/css") 2 @ Scripts.Render ("~ / bundles/js")

Isn't it convenient! For more information about Bundle, please refer to

Http://www.asp.net/mvc/overview/performance/bundling-and-minification

Because it is not our main content today, it is just compared with the static file processing in Nancy, which is easy for us to understand.

Let's take a look at what to do with static files in Nancy.

For demonstration purposes, only css is used here.

First look at the specific use, and then briefly analyze its internal implementation.

Create a new empty asp.net application

Add the references we need to this application, which can be based on the

Add Nancy-related references in any way you like

Second, establish Modules

Old rules: Modules folder, HomeModule.cs

1 public class HomeModule: NancyModule 2 {3 public HomeModule () 4 {5 Get ["/"] = _ = > 6 {7 return View ["index"]; 8}; 9 10 Get ["/ default"] = _ > 11 {12 return View ["default"]; 13} 14 15 Get ["/ custom"] = _ = > 16 {17 return View ["custom"]; 18}; 19 20 Get ["/ other"] = _ = > 21 {22 return View ["other"]; 23} 24 25 Get ["/ sub"] = _ > 26 {27 return View ["sub"]; 28}; 29} 30}

Third, create three new folders: content, assets and other, and create a new sub folder under the assets folder to store style sheets. Fourth, add some simple styles to these folders.

The content of sytle.css under content is as follows

1 body {background-color:#00ffff;} 2 p {font-size:xx-large;}

The style.css under assets and other is as follows

1 body {background-color:#00ffff;} 2 p {font-size:xx-large;color:#ff0000;}

The content of style.css under assets/sub is as follows

1 body {background-color:#808080;} 2 p {font-size:xx-large;color:#ff0000;} 5. Add Views

Old rule: Views folder, Home folder

Add five pages: index.html, default.html, custom.html, other.html and sub.html

Index.html

Default.html

Custom.html

Other.html

Sub.html

Configure Convention in the bootstrapper (a crucial step)

Create a new DemoBootstrapper.cs to inherit DefaultNancyBootstrapper and override our ConfigureConventions

1 public class DemoBootstrapper: DefaultNancyBootstrapper2 {3 protected override void ConfigureConventions (NancyConventions nancyConventions) 4 {5 base.ConfigureConventions (nancyConventions); 6 nancyConventions.StaticContentsConventions.Add (StaticContentConventionBuilder.AddDirectory ("assets")); 7} 8}

7. Operation result

VIII. Analysis and discussion of the results

1. The style used by default.html is under content and can load the style normally!

2. The style used by custom.html is under assets and can load the style normally!

3. The style used by other.html is under other, so you can't load the style normally!

4. The style used by sub.html is under assets/sub and can load the style normally!

Obviously, the result was a little unexpected, we only configured one item in the configuration of Convetion!

Is to deal with the assets folder. Nothing else is manually configured!

But the style under the content can be displayed normally! And the other below can not be displayed properly! The style of the subfolder sub of assets is also displayed normally!

This doesn't seem to be very reasonable.

If you take a look at the contents of Network, you will find that the stylesheet under other is not as simple as not loading normally, but directly gives a 404 upload!

Then let's take a closer look at what's going on here.

Fork a copy of the Nancy source code, clone to the local, to see why. (in fact, I added a Demo to the source code in the above example)

First of all, let's take a look at what's under our topic today, Conventions.

Among them, we can see from the name that there are 7 static files related to our theme today!

But this is not our starting point, our starting point is the following!

1 protected override void ConfigureConventions (NancyConventions nancyConventions) 2 {3 base.ConfigureConventions (nancyConventions); 4 nancyConventions.StaticContentsConventions.Add (StaticContentConventionBuilder.AddDirectory ("assets")); 5}

The configuration of Convention guides us to take a look at the NancyConvetions class first.

The BuildDefaultConventions method is called in its constructor

1 / 2 / / Initializes a new instance of the class.3 / 4 public NancyConventions () 5 {6 this.BuildDefaultConventions (); 7}

This clearly tells us that, in any case, it will have a default Conventionsgiving! And looked at the implementation in it.

You will find that the default Convention is not just one! It contains more than one. Here we only talk about static files.

1 private void BuildDefaultConventions () 2 {3 var defaultConventions = 4 AppDomainAssemblyTypeScanner.TypesOf (ScanMode.OnlyNancy); 5 this.conventions = defaultConventions 6. Union (AppDomainAssemblyTypeScanner.TypesOf (ScanMode.ExcludeNancy)) 7. Select (t = > (IConvention) Activator.CreateInstance (t)) 8 foreach (var convention in this.conventions) 9 {10 convention.Initialise (this); 11} 12}

Now it's time to find the default Convetion for static files.

I found that among the seven correlations just now, there is one DefaultStaticContentsConventions.

It implements the IConvention interface (Nancy is basically interface programming, very niceworthy!) .

In the initialization method

1 public void Initialise (NancyConventions conventions) 2 {3 conventions.StaticContentsConventions = new List4 {5 StaticContentConventionBuilder.AddDirectory ("Content") 6}; 7}

Is it almost the same as our custom configuration! I want to see the parameter "Content" of AddDirectory, and everyone should know it.

Why the styles under our content can be loaded normally without configuration (I'll go, it defaults to content, can it be loaded abnormally.)

Who is the StaticContentConventionBuilder inside?

This is a static directory-based help class

There are two main methods AddDirectory and AddFile, both of which return things of type Func.

If you look at the name, you already know what has been implemented, one based on a directory and one based on a single file.

You need to pay attention to the parameters of these two methods here!

There are other things for stitching directories and dealing with Cache.

Taking a look at these important classes, is it clear about the default configuration of this static file?

Then the understanding of the custom Convetion configuration is similar, so it is no longer cumbersome here.

As you can see from the ConfigureConventions of the bootstrapper, no matter how many Convetion we customize

Are to be added to the StaticContentsConventions collection.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report