If you need to define the new resource (script or stylesheet):
- Create a class inheriting IResourceManifestProvider
- Provide the void BuildManifests(ResourceManifestBuilder builder) method and
- Add all necessary resources via builder.Add().DefineStyle("") or builder.Add().DefineScript(...), just as you noted in your question.
For example:
public class ResourceManifest : IResourceManifestProvider {
public void BuildManifests(ResourceManifestBuilder builder) {
var manifest = builder.Add();
manifest.DefineStyle("MyStyle").SetUrl("mystyle.css");
manifest.DefineScript("MyScript").SetUrl("myscript.js").SetDependencies("jQuery");
}
}
This defines one style and script you can reuse in your views. Urls are relative to /Styles (or /Scripts) folders in your theme/module where the class is located.
If you want to reuse some of resources already defined (in all enabled modules and themes), it's as easy as writing eg.:
...
@{
Style.Require("MyStyle").AtHead();
Script.Require("MyScript").AtFoot();
}
...
inside your .cshtml view file. Example above would inject mystyle.css and myscript.js at appropriate locations (header/footer of the final page).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…