In the previous article we introduced the route in Nancy, which we'll introduce to the view engine in Nancy.
in ASP. NET MVC, we use the Viewresult class to return a view. Nancy also provides similar functionality in the NancyModule
class, where Nancy provides a ViewRenderer
type of view property to return to the view.
The Viewrenderer class code is as follows, with three property accessors available in the class
Let's change the previous HelloModule.cs, add a/hello route template, and use the View property to return a view
Now that we start the project and enter/hello, the browser returns the following results.
Do not panic, this indicates that our request was processed by Nancy, just because the server did not find hello.html this file correctly, so error, continue to read the next section of the content, your page will be displayed correctly.
View location conventions in Nancy The location conventions for several views are described in Nancy's official website document, which defines the order and manner in which the search view files are located in Nancy.
Let's take the following code as an example
public class HelloModule : NancyModule { public HelloModule() { Get("/hello", p => View["hello.html"]); } }
When/hello requested to get into Nancy's pipeline, we decided to use hello.html as the response page, and when Nancy tried to find the page,
- Nancy first looks for the "/views/[module name/[specified page file name]" in the root directory of the site, and if the file is found, Nancy reads the page content and binds the data model back to the client, which is the view and Module name convention. In the current example,/views/hello/hello.html
- If the file is not found, Nancy will continue to look for the "/[module Name []/[specified page file name]" in the root directory of the site, which is the module name convention. The current example is/hello/hello.html
- If you still can't find the file, Nancy will continue to look for "/views/[specified page file name" under the root of the site, which is the view Folder name convention. The current example is/views/hello.html
- If you still can't find the file, Nancy will eventually look for the "/[specified page file name" in the root directory of the site, which is the root convention. The current example is/hello.html
- If none is found, Nancy returns an error page
Switch Web site root directory In. NET core we typically use static files in the Wwwroot directory, not the Web site root directory, which causes Nancy to incorrectly access the correct static files. Here we need to modify Nancy's default site root settings.
Nancy we can implement a IRootPathProvider
custom Web site root by implementing the methods of the interface GetRootPath
and overriding DefaultNancyBootstrapper
the properties in the class RootPathProvider
.
First we create a new class CustomRootPathProvider
.
public class CustomRootPathProvider : IRootPathProvider { public string GetRootPath() { return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot"); } }
By stitching the directory in the code, we point to the root directory of the current Web site Wwwroot.
Then we create a new class CustomBootstrapper
that inherits NancyDefaultBootstrapper
the class and uses CustomerRootPathProvider
the Rootpathprovider as Nancy.
public class CustomBootstrapper : DefaultNancyBootstrapper { protected override IRootPathProvider RootPathProvider { get { return new CustomRootPathProvider(); } } }
Special Note: When you create a custom bootstrapper, the trace function of the previous Nancy is masked, and if you want to re-enable the trace feature, you need to add the following code to the Custombootstrapper.
public override void Configure(INancyEnvironment environment) { environment.Tracing(enabled: true, displayErrorTraces: true); base.Configure(environment); }
Now we restart the project, enter/hello, the page is displayed correctly.
The view engine supported in Nancy The view engines supported in Nancy are as follows
- Super Simple View Engine (ssve)
- Razor
- Spark
- Ndjango
- Dotliquid
Here we highlight Ssve.
Super Simple View engine Super simple view engine, referred to as ssve, is the view engine provided by Nancy by default, and our previous example uses the Ssve view engine.
Here we introduce some basic syntax for a ssve view engine
The value of the input variable Grammar:
@Model[.Parameters]
Cases:
BookModule.cs
public class BookModule : NancyModule { public BookModule() { Get("/books/{bookId}", p => View["book.html", new { BookId = p.bookId }]); } }
Book.html
The Book Id is @Model.BookId
Iteration Grammar:
@Each[.Parameters] [@Current[.Parameters]] @EndEach
Cases:
BookModule.cs
public class BookModule : NancyModule { public BookModule() { Get("/books", p => { return View["books.html", new { Books = new List<Book> { new Book("S001", "Math 101"), new Book("T001", "C# Programming") } }]; }); } } public class Book { public Book(string isbn, string bookName) { ISBN = isbn; BookName = bookName; } public string ISBN { get; set; } public string BookName { get; set; } }
Books.html
<table> <tr> ISBN </tr> <tr> Book Name </tr> <tbody> @Each.Books <tr> <td> @Current.ISBN </td> <td> @Current.BookName </td> </tr> @EndEach </tbody></table>
Conditions Grammar
@If[Not].Parameters [contents] @EndIf
Note: Only variables of type bool are supported, expressions are not supported
Cases:
BookModule.cs
public class BookModule : NancyModule { public BookModule() { Get("/books", p => { return View["booksWithIf.html", new { Books = new List<Book> { new Book("S001", "Math 101", true), new Book("T001", "C# Programming", false) } }]; }); } } public class Book { public Book(string isbn, string bookName, bool isNew) { ISBN = isbn; BookName = bookName; IsNew = isNew; } public string ISBN { get; set; } public string BookName { get; set; } public bool IsNew { get; set; } }
Bookswithif.html
<table> <tr> ISBN </tr> <tr> Book Name </tr> <tbody> @Each.Books @If.IsNew <tr> <td> @Current.ISBN </td> <td> @Current.BookName </td> </tr> @EndIf @EndEach </tbody></table>
Output partial View Grammar
@Partial['<view name>'[, Model.Property]]
Cases:
BookModule.cs
public class BookModule : NancyModule { public BookModule() { Get("/books/{bookId}", p => View["bookWithPartial.html", new { BookId = p.bookId }]); } }
Partial.html
Bookwithparital.html
The Book Id is @Model.BookId@Partial['partial.html']
Template page Grammar:
@Master['<name>']@Section['<name>']@EndSection
Cases:
BookModule.cs
public class BookModule : NancyModule { public BookModule() { Get("/books/{bookId}", p => View["bookWithMaster.html", new { BookId = p.bookId }]); } }
Bookwithmaster.html
@Master['master.html']@Section['content']The Book Id is @Model.BookId@EndSection
Master.html
Razor Although the Ssve view engine is simple, there are not many methods available, and there are many complex scenarios that need to be custom syntax templates.
In addition to Ssve, Nancy can also use the same Razor view engine as in ASP. NET MVC, where syntax and usage are essentially the same as the Razor engine in ASP. However, it is important to note that Nancy.Viewengines.Razor has not finished rewriting for. NET Stardard, so if you want to use the razor engine in Nancy, you can only use it in a non-. NET core project, see the official website
Summarize Nancy's view engine has not been released since 2016.12 months later, and the personal sense that Nancy is now developing the focus is no longer on the view engine, Nancy more as a webapi to use, recently found a book, introduced Nancy's microservices practice, Interested students can read together.
MicroServices in. NET Core with Examples in Nancy
This blog source code
Next we'll learn about data model bindings in Nancy.
Lamond Lu
Source: www.cnblogs.com/lwqlun/p/9629185.html
This site uses the "Attribution 4.0 International" Creative sharing agreement, please indicate the author and source in the obvious position of the article.