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

What is the use of ASP.NET MVC2 framework

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

Share

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

This article mainly introduces "what is the use of the ASP.NET MVC2 framework". In the daily operation, I believe that many people have doubts about the use of the ASP.NET MVC2 framework. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what is the use of the ASP.NET MVC2 framework?" Next, please follow the editor to study!

1. About caching

Caching on the data tier is necessary, needless to say.

Another important thing is the view clip cache.

View fragment caching suitable for ASP.NET MVC (part 1): get started

View fragment caching suitable for ASP.NET MVC (middle): more practical API

View fragment caching method suitable for ASP.NET MVC (part two): page output principle

I wanted to use Lao Zhao's, but I found a new feature in ASP.NET MVC 2: Html.Partial can return the generated HTML, and the return type is: MvcHtmlString. Although I have to use Partial View to generate Html fragments, I think this is enough for me, so I made such a Helper, mainly caching the generated HTML fragments into Memcached. The code is as follows:

Public static class MvcHtmlHelper {public static MvcHtmlString MemcacheHtmlPartial (this HtmlHelper htmlHelper,int duration, string partialViewName, object model, ViewDataDictionary viewData) {object obaear = htmlHelper.ViewContext.RouteData.DataTokens ["area"]; string area=string.Empty; if (obaear! = null) area= obaear.ToString (); string key = string.Format ("MemcacheHtmlPartial_ {0} {1}", area, partialViewName); object ob = DistCache.Get (key); if (ob = = null) {MvcHtmlString mstr = htmlHelper.Partial (partialViewName, model, viewData) DistCache.Add (key, mstr.ToString (), TimeSpan.FromSeconds (duration)); return mstr;} else {return MvcHtmlString.Create ((string) ob);}

Then, I think, in this way, at each request, we still have to take the data out of the Controller and then transmit it to the Partial View. Now that it's cached, you don't have to pull the data out of Controller every time you request it! Although there will be caching in the data layer.

So I, can no longer save the controller to get data consumption, so there is the following code, its function is: cache Action generated HTML to Memcached.

Public static MvcHtmlString MemcacheHtmlRenderAction (this HtmlHelper htmlHelper, int duration, string actionName,string controllerName, RouteValueDictionary routeValues) {object obaear = htmlHelper.ViewContext.RouteData.DataTokens ["area"]; string area = string.Empty; if (obaear! = null) area = obaear.ToString (); string key = string.Format ("MemcacheHtmlRenderAction_ {0} {1} {2}", area, controllerName,actionName); object ob = DistCache.Get (key); if (ob = = null) {/ / htmlHelper.RenderAction (actionName, controllerName, routeValues) StringWriter writer = new StringWriter (CultureInfo.CurrentCulture); ActionHelper (htmlHelper, actionName, controllerName, routeValues, writer); string wStr = writer.ToString (); DistCache.Add (key, wStr,TimeSpan.FromSeconds (duration)); MvcHtmlString mstr = MvcHtmlString.Create (wStr); return mstr;} else {return MvcHtmlString.Create ((string) ob);}}

To be clear, the method of Actionhelper is extracted from the original code of MVC. Because the Html.RenderAction method in MVC2 does not return an overloaded version of MvcHtmlString. Who has a better way?

In fact, Action in MVC has an output cache, so using Html.RenderAction directly in View can solve a lot of problems. This is mainly that you can use the program to manage the cache.

two。 About the placement of static content

Traditionally, static content is placed in the same directory as the mvc program, such as js,css, uploaded images, and so on. But in this case, all static requests have to be processed by aspnet_isapi, which is very uneconomical. So static content is generally placed on another subdomain. Http://www.86e0.com/t is put on cdn.86e0.com.

3. About strongly typed ViewModel

I basically read Lao Zhao's Asp.net MVC*** practice. One of them is strongly recommended to use strongly typed ViewModel. I tried some pages and found that using strongly typed ViewModel is not suitable for me at this stage. Because I use NbearLite, most of what I grab from the database is DataTable. I think DataTable+NbearLite is quite convenient, although it is not convenient to access data in dynamic language, but it can save a lot of code than using Entity,ViewModel, DTO, and so on. Then, the most important thing is, because my station often changes, so the database changes, add fields, minus fields is a very common thing. However, using NbearLite + DataSet,DataTable is very convenient.

So I think, if you don't use DDD,DDT to do Asp.net MVC, you can still use DataTable. Because DDD,DDT still costs a little bit to learn.

4. About URL Generation

When URL was generated, Lao Zhao wrote a series of articles:

Performance comparison of various URL generation methods

Performance comparison of various URL generation methods (conclusion and analysis)

Generate a smooth design interface (Fluent Interface) for URL

Performance optimization results of URL generation method

I choose directly.

Raw way, the fastest, is suitable for me. Heh. Not a strong type is suitable for me.

Share a very useful Asp.net MVC paging Helper.

The effect is as follows:

Please pay attention to the generated URL, which is used? Parameter = the way of the page number. The code is as follows:

/ paging Pager display / QueryStringKey / / identifying the current page number / Total data per page / public static string Pager (this HtmlHelper html, string currentPageStr, int pageSize, int totalCount) {var queryString = html.ViewContext.HttpContext.Request.QueryString; int currentPage = 1; / / current page if (! int.TryParse (queryString [currentPageStr], out currentPage)) currentPage = 1 / bind to the corresponding QueryString var totalPages = Math.Max ((totalCount + pageSize-1) / pageSize, 1); / / Total pages var dict = new RouteValueDictionary (html.ViewContext.RouteData.Values); var output = new StringBuilder (); foreach (string key in queryString.Keys) if (queryString [key]! = null & &! string.IsNullOrEmpty (key) dict [key] = queryString [key] If (totalPages > 1) {if (currentPage! = 1) {/ / process home page connection dict [currentPageStr] = 1; output.AppendFormat ("{0}", html.RouteLink ("home page", dict));} if (currentPage > 1) {/ / process previous page connection dict [currentPageStr] = currentPage-1; output.AppendFormat ("{0}", html.RouteLink ("previous page", dict)) } else {output.AppendFormat ("{0}", "previous page");} int currint = 5; for (int I = 0; I = 1 & & (currentPage + I-currint)

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

Development

Wechat

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

12
Report