First, preface in MVC, the region (area) and the partial view (Partialview) should be the two things we use very frequently today we are in Nancy, the two things in a simple use! Second, the simple use of regional areas, both for the development process or code management occupies an important position! Let's take a look at how the region in Nancy is going to be used, in fact, Nancy did not explicitly put forward the concept of the area, but also I have a good understanding of the concept with the MVC contrast, I added to look at it, here to use the Nancy, Nancy.Hosting.Aspnet These references are common to all view engines, so don't add a special view engine (Razor, spark, etc.) first to see our project framework we define three Homemodule, respectively, in a different location, Admin , other, root directory this is equivalent to having the Admin, other, root directory three regions specific content as follows: Admin/homemodule.cs
1 Public classHomemodule:nancymodule2 {3 PublicHomemodule ():Base("/admin")4 {5get["/"] = _ ~ =6 {7 returnview["Index"];8 };9 }Ten}
Other/homemodule.cs
1 Public classHomemodule:nancymodule2 {3 PublicHomemodule ():Base("/other")4 {5get["/"] = _ = = {returnview["Index"]; };6 }7}
HomeModule.cs
1 Public classHomemodule:nancymodule2 {3 PublicHomemodule ()4 {5get["/"] = _ ~ =6 {7 returnview["Index"]; 8 };9 }Ten}
Similarly, we define the view of the corresponding area! Specific as follows: views/admin/home/index.html
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <title></title>5 <MetaCharSet= "Utf-8" />6 </Head>7 <Body>8 Admin->home->index9 </Body>Ten </HTML>
views/other/home/index.html
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <title></title>5 <MetaCharSet= "Utf-8" />6 </Head>7 <Body>8 Other->home->index9 </Body>Ten </HTML>
views/home/index.html
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <title></title>5 <MetaCharSet= "Utf-8" />6 </Head>7 <Body> 8 Home --Index9 </Body>Ten </HTML>
This makes it easy to separate different functions into different areas, the following to see the effect is not very simple! In fact, with our normal usage does not have much difference! Just a few more folders, easy to manage. We can also specify the location of the view in the module, but it is easy to distinguish between the good point, the purpose or management convenience.The details of the view engine in Nancy can help us to understand more clearly the loading of views and so on, as this piece has already been told by the park friendsI'm not going to repeat it here.. NET Nancy Detailed (iii) Respone and ViewengineThird, the simple use of the partial view of the partial view this piece, I mainly talk about razor (because of personal use more) usage is very simple @html.partial (viewname,modelforpartial) is not consistent with the basic MVC? We now define two partial views, the first _partialheader.cshtml and the tail _partialfooter.cshtml
1 <div> header </div>
1 <div> tail </div>
Then define a layout page _layout.cshtml
1@inherits nancy.viewengines.razor.nancyrazorviewbase<Dynamic>2<! DOCTYPE html>3 456<meta name="Viewport"Content="Width=device-width"/>7<title> @ViewBag .title</title>89<body>Ten@Html. Partial ("/partials/_partialheader") One<div> A @RenderBody () -</div> -@Html. Partial ("/partials/_partialfooter") the</body> -The following script module returns a view index.cshtml1 @{2 " views/_layout.cshtml " ; 3 }4 5 Content
Run results This is a static partial page, below we bind the model, that is, in the partial with two parameters to write a partial view _partialtime.cshtml1 <span> @Model </span>
Add the following code to the _layout.cshtml just now1 @Html. Partial ("/partials/_partialtime", Model)
Modify Homemodule at the same time1 get["/"] = _ = = 2 {3 var model = DateTime.Now.ToString (); 4 return view["index2", model]; 5 };
Run OK, exactly what we're looking for. Note: If you do not add @inherits to the page nancy.viewengines.razor.nancyrazorviewbase<dynamic>Our smart tip is not to appear partial!! Partial is implemented through HtmlHelper, but HtmlHelper in Nancy has a much less usable type than MVC. Like @Html. ActionLink () These are not.Use of Nancy's regions and partial views