Tips for ASP. NET mvc3: local view and Cache

Source: Internet
Author: User
ArticleDirectory
    • Let's talk about the paging bar first.
    • Next, sidebar
    • Check whether the cache is valid

In the previous article, I created a blog project. Today I plan to continue to work on this project.

If you do not know the layout of ASP. NET mvc3, you can take a look at this article and it will be helpful to you.

I will mainly talk about my understanding of the partial view and cache in ASP. NET mvc3. I will not talk about a few images first.

The first figure is the blog homepage page, and the second figure is the blog sidebar. I use two local views provided by ASP. NET mvc3 to implement them. Someone may ask, why are you using two different methods? Isn't it 13? .... I think I can only use the "Haha" method. Of course, I chose the method as needed.

The third figure shows the Page Structure after completion.

Let's talk about the paging bar first.

What data do we need to make it into the navigation bar above? 1. Current page number 2. How many articles are displayed per page 3. How many articles are in the database

Okay. As long as this is needed, we can provide it to homecontroller through viewbag in its indexaction.

Public class homecontroller: controller {blogdb DB = new blogdb (); // get:/home/page/5 Public actionresult index (INT page = 1) {int countperpage = 10; vaR posts = dB. posts. include ("category "). include ("tags "). include ("Comments "). orderbydescending (P => P. createtime ). skip (page-1) * countperpage ). take (10); viewbag. currentpage = page; viewbag. countperpage = countperpage; viewbag. postscount = posts. count (); Return view (posts );}}

If data is available, let's start creating a local view.

Right-click the share folder in the view folder and select add view. Here I name it _ pagination. Note:

Tip: in ASP. NET mvc3, if a view starts with "_", it cannot be accessed from outside. It can only be nested as a local view in other views.

Open the created view and add the followingCode

@ {Int pagecount = viewbag. postscount/viewbag. countperpage + 1; int currentpage = viewbag. currentpage ;} <Div class = "pagination"> <SPAN class = "first_page"> <a href = "/"> first </a> </span> @ for (VAR I = 1; I <= pagecount; I ++) {if (I = currentpage) {<SPAN class = "page_num current_page"> <a href = "/page/@ I"> @ I </a> </span>} else {<SPAN class =" page_num "> <a href ="/page/@ I "> @ I </a> </SPAN >}@ if (currentpage = pagecount) {<SPAN class = "next_page"> <a href = "/page/@ pagecount"> next </a> </span>} else {<SPAN class = "next_page"> <a href = "/page/@ (currentpage + 1) "> next </a> </SPAN >}</div>

We call this local view in the home index view.

@ Model ienumerable <blog. models. post >@{ viewbag. title = "ninofocus | Notepad";} <Div id = "Main"> @ HTML. partial ("_ postlist", model) <! -- Here, we can see that we call him through the following statement --> @ html. Partial ("_ pagination") </div> <! -- # End main -->

Well, the paging bar is complete.

Next, sidebar

Sidebar is different from the paging bar, because the paging bar is only displayed on the home page, and all the home pages can provide data to him. Sidebar does display any page, so he must provide his own data.

Well, someone may think, isn't it the same as above? I write some server-side code on the page. Will it be hard to provide data?

Just like this

<Li> <H4 class = "H4"> <span> some cool <strong> websites </strong> </span> </H4> <ul> @ foreach (VAR link in new blog. models. blogdb (). friendlinks) {<li> <a href = "@ link. URL "Title =" @ link. name "> <strong> @ link. name </strong> </a>-@ link. description </Li >}</ul> </LI>

Will it die? Of course not. This is also a solution (although I do not recommend it ). However, I think it is inappropriate. The update frequency of Sidebar data is very low. In this way, every time you refresh the page or browse other pages, you have to re-request the database. Isn't that right?

In order to make it appropriate, but also fully squeeze ASP. net mvc features. I decided to use childaction.

Add a childactioncontroller, which is mainly responsible for some partial views (actually providing data ). The following uses the links in the sidebar as an example to describe the code of the action.

[Childactiononly] [outputcache (duration = 3600)] public partialviewresult sidebarfriendlinks () {var links = dB. friendlinks; return partialview (LINKS );}

[Childactiononly] attribute indicates that this action can only be called internally and cannot be accessed by the outside.

[Outputcache] Enable cache. The duration field is the cache time, in seconds. Here I set it to 1 hour.

For the type of the return value of your healthy action, use partialviewresult instead of commonly used actionresult. Otherwise, many brain cells will die, and I was killed last night. ASP. net mvc follows the dry (don't repeat yourself) principle. If you do not specify a template page for the view of actionresult, it automatically adds the default template page to you. The default template page can be specified in the _ viewstart. cshtml File

 
@ {Layout = "~ /Views/shared/_ layout. cshtml ";}

If you read the article on ASP. NET mvc3 layout recommended above, you will know that this is a good thing.

Let's simulate the call of the page. We call sidebarfriendlinks on the page where the template page is used, then sidebarfriendlinks calls the template, and then calls sidebarfriendlinks on the template page .... Then you will suffer.

If you must use actionresult as the return value, add the following statement to the local page.

 
@ {Layout = NULL ;}

Therefore, to be healthy, use partialviewresult.

Well, let's continue. Right-click sidebarfriendlinks and select add view.

Let's open the sidebarfriendlinks. cshtml file, modify the @ model statement, and add the Page code.

@ Model ienumerable <blog. models. friendlink> <li> <H4 class = "H4"> <span> some cool <strong> websites </strong> </span> </H4> <ul> @ foreach (VAR link in Model) {<li> <a href = "@ link. URL "Title =" @ link. name "> <strong> @ link. name </strong> </a>-@ link. description </Li >}</ul> </LI>

Then call _ sidebar. cshtml

<Div class = "sidebar"> <ul> @ HTML. action ("sidebartags", "childaction") @ HTML. action ("sidebararchive", "childaction") @ HTML. action ("sidebarfriendlinks", "childaction") </ul> </div> <Div class = "clear"> </div> <! -- End sidebar -->

After the sidebar is written in this way, can I use the configuration file to control the content in it? Hey !!!

Finally, we provide the code for _ layout. cshtml. Just ask you to have a clearer understanding of the page structure of this blog.

<! Doctype HTML>  

@ Renderbody () is where other pages are filled.

@ Rendersection ("footerscript", false) reserved space. The first parameter indicates the name. If the second parameter is set to false, it indicates that not all pages must add content to it; if it is true, it indicates that all pages using this template must be filled in.

On the page, you can call

 
@ Section footerscript {... JavaScript here}
Check whether the cache is valid

Open SQL Server Profiler to monitor database requests.

1. cache disabled

When cache is not enabled, each time I refresh the blog homepage, there will be 5 database requests (three partial pages in my sidebar)

Why is the same SQL statement requested twice when EF is used? Solution !!! I will calculate it first for the moment.

2. When cache is enabled

Now I enable the sidebar cache to check the number of database requests refreshing the page.

The number of requests has changed from 5 to 2. Indicates that the cache is successfully enabled. Haha, it's a bit refreshing.

So I added the homepage to the cache.

/// Get:/home/page/5 [outputcache (duration = 60*5)] public actionresult index (INT page = 1)

Refresh the page now.

No database connection request.

It seems that the homepage of the blog garden is also cached, as if it were a minute or two.

Tags: ASP. NET MVC
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.