【转】ASP.NET MVC 的最佳实践
[This post is based on a document authored by Ben Grover (a senior developer at Microsoft). It is our intention to integrate this information into the MVC 3 documentation on MSDN. We hope to hear from you and welcome any suggestions you might have.]
This document presents a set of coding guidelines aimed at helping the ASP.NET MVC developer create solid applications. Of course, it's up to you as the developer to decide which of these guidelines are appropriate for your application.
Model Recommendations
The model is where the domain-specific objects are defined. These definitions should include business logic (how objects behave and relate), validation logic (what is a valid value for a given object), data logic (how data objects are persisted) and session logic (tracking user state for the application).
DO separate the model its own project with a distinct assembly.
For applications with a large complex model, it's a good idea to create a separate assembly for the model to avoid accidently mixing concerns. You can then reference the model assembly in your ASP.NET MVC project.
DO put all business logic in the model.
If you put all business logic in the model, you shield the view and controller from making business decisions concerning data. You also reap the following benefits:
- Less duplicated business logic.
- The view is easier to read when there is no business logic present.
- Testing business rules is isolated to the model.
For example, if you have a business requirement to display a user's name with the last name first, you could put the logic in the view as follows:
<% if (String.Compare((string)TempData["displayLastNameFirst"], "on") == 0) { %> Welcome, <%= Model.lastName%>, <%= Model.firstName%> <% } else { %> Welcome, <%= Model.firstName%> <%= Model.lastName%> <% } %>
However, you would have to duplicate this logic in every place this business requirement was needed. Instead, you could put the business logic the the "display last name first" rule in the model by adding a property to the model that encapsulates the business logic as follows:
public string combinedName { get { return (displayLastNameFirst ? lastName + " " + firstName : firstName + " " + lastName); } private set { ; } }
This would greatly simplify the view as shown:
<% Welcome, <%= Model.combinedName %> %>
DO put all validation logic in the model.
All input validation should occur in the model layer. This includes client side validation, which is essential to performance. However, client side validation can be circumvented (with, for example, tools like Fiddler).
You can use ModelState to add validation checking. The following example shows how to add validation checks to ModelState explicitly:
if (String.IsNullOrEmpty(userName)) { ModelState.AddModelError("username", Resources.SignUp.UserNameError); }
However, given the advances in .NET Framework, the System.ComponentModel.DataAnnotations should be the preferred method for validation. These annotations are added as attributes to the properties of a model class, as the following example shows:
public class User { [Required(ErrorMessageResourceName = "nameRequired", ErrorMessageResourceType = typeof(Resources.User))] public String userName { get; set; } ... }
DO define interfaces for data access
.
It is preferred that interfaces be used to expose methods on a provider for data access. This reinforces the loosely coupled component design of ASP.NET MVC.
Consider using the Entity Framework or LINQ to SQL as the means of creating wrappers around calls to a database. Both Entity Framework and LINQ to SQL allow the use of stored procedures as well.
DO put all session logic in the model.
It is beyond the scope of this document to explore in depth the various mechanisms for storing session state in the model. As a starting point, here are a few of possibilities of session state storage:
Technique | Strengths | Weaknesses |
---|---|---|
In Process | No additional setup required. | Does not work if the web site needs to scale. |
Session State Service | Lightweight sertvice runs on each machine in a web farm. Faster than database session storage. |
Session data is lost if the service goes down. |
Database | Session data is persisted. | Slower than session state. Management cost is relatively high. |
View Recommendations
The primary concern of a view is presentation of the model. The view is chosen by the controller. Business logic does not belong in the view, because business logic is the concern of the model layer. The view mechanism can be extremely flexible. For example, a view of the model for a web page can be rendered using HTML. Another view of the same model (same data), can be presented in XML and act as a web service.
DO put HTML in Views and Partial Views (not in a controller).
A strength of the view pattern is the readability of view template files. For the default view engine, ASP.NET offers the following types of view files: full HTML view (.aspx), partial HTML view (.ascx), and master page (.master). A master page enables you to specify an overall layout for a view. Master pages can be nested several times to create a hierarchy of available layout types.
The following example, shows a view which calls a partial view:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> … Below is a list of items submitted by <b> <%= Html.Encode(ViewData["name"]) %></b>. <p> ... <div id="items"> <% Html.RenderPartial("ItemsByName");%> </div> </asp:content>
The partial view (ItemsByName.ascx) is shown here:
<%@ Control Language="C#" %> … <% foreach (Seller.Controllers.Items item in (IEnumerable)ViewData.Model) { %> <tr> <td> <%= Html.Encode(item.title)%> </td> <td> <%= Html.Encode(item.price)%> </td> </tr> <% } %> </table> <% } %>
The partial view is a powerful extensibility and reuse mechanism. For example, we can include the same view in an admin view, without writing another line of code.
DO access data in views using ViewData.
ASP.NET provides the following mechanisms to enable you to access data from view templates:
- ViewData.Model object, which is setup in a controller’s action method by passing a model object in the action method's return statement (
return View(myModelObject)
). - ViewData dictionary (property bag), which enables you to enter any data into the dictionary in an action method (ViewData[“key”] = value), and then access that same dictionary from within the view.
Whenever possible, you should use ViewData Model instead of the ViewData dictionary because it provides better type safety. Additionally, you should use either data access mechanism rather than accessing the Request/Session state directly in the view template.
If you have an object for which you are going to display multiple properties, you should use ViewData.Model and create a strongly typed view for that object type. If you have a seller’s details page, for example, and the seller class has the name, phone, address, email, etc. properties. Then you would assign a seller instance to ViewData.Modelin the controller before rendering the view. If you have disparate data such as page #, a user’s name, and the current time, use ViewData dictionary.
Avoid data access in the view when using model binding. In other words, do the data retrieval from the database in the controller. Then populate lightweight view model objects from the retrieved data in the controller before executing the view. So the lightweight view model objects do not retrieve data during view execution.
DO enable (auto-generated) client side validation.
Previously, web developers were faced with the dilemma of keeping client and server validation synchronized. Starting with ASP.NET MVC 2, it has become easy to add client side validation.
To add client side validation:
- Add data validation in the model layer as described in the section titled DO put all business login in the model.
- Ensure the following javascript files are in Scripts directory in your project:
- MicrosoftAjax.js
- MicrosoftMvcValidation.js
- Add the following lines to your form submission page:
<script src="<%= Url.Content("~/Scripts/MicrosoftAjax.js") %>" type="text/javascript"></script> <script src="<%= Url.Content("~/Scripts/MicrosoftMvcValidation.js") %>" type="text/javascript"></script>
- Put the following line before the form tag:
<% Html.EnableClientValidation(); %>
Now if you try to edit a form field which does not match the data annotation, the client side validation will fail and immediate feedback is given to the user.
DO insert server-side comments in templates.
Use server side comments in the view templates for commenting. These comments are stripped out before the server returns the HTML representation.
The following line demonstrates a server side comment:
<%-- This is a server side template comment --%>
Do not use HTML comments in the view template because they will be rendered to the web browser and could be viewed by an advanced (and potentially malicious) user.
DO use HTMLHelper extension methods.
The System.Web.Mvc.Html class contains useful HTML extension methods. These extension methods include helpers for:
- Form generation (BeginForm)
- Input field generation (checkbox, hidden, radio button, textbox)
- Link generation (ActionLink)
- XSS protection (Encode)
These HTML helpers should be utilized as much as possible. For example, the following code creates a link using the route table back to the default action on the controller from which the view is called.
<%= Html.ActionLink(“Home page”, “Default”) %>
Controller Recommendations
The controller (and a specified action method) is invoked by the routing system given a pattern match on the URL. The controller receives input from the routing system, which includes the HTTP request context (session, cookies, browser, etc).
DO use model binding instead of manually parsing the request.
ASP.NET MVC abstracts much of the rote code of object deserialization by using model binding. Model binding is a mechanism by which request context data is marshaled through reflection into the object type defined on the action method.
The following example shows a Seller class that defines the data that might be submitted from a form for signing up sellers:
public class Seller { public Int64 ID { get; set; } public string Name { get; set; } public string Phone { get; set; } public string Address { get; set; } }
The form form submitting the sellers data could be contained in a Register view with code similar to the following:
<% using (Html.BeginForm()) { %> <legend>Account Information</legend> <p> <%= Html.TextBox("Name") %> </p> <p> <%= Html.TextBox("Phone") %> </p> <p> <%= Html.TextBox("Address") %> </p> <p> <input value="Register" type="submit" /> </p> <% } %>
The controller would need a Register action method that would provide the model binding as shown:
public ActionResult Register([Bind(Exclude="ID")] Seller newSeller) { ... }
The default model binder will look for each property in the class given the following order (using Name as example):
Request.Form["Name"], if it exists
RouteData.Values["Name"], if it exists
Request.QueryString["Namel"], if it exists
- Otherwise,
null
As you can see from the Register action method, there are several attributes which can be placed on an object that will be invoked using the default model binder.
The model binding system also runs the validation logic that has been applied to the model object, such as the data annotations attributes.
The model binding system has a rich extensibility mechanism that allows full customization of how objects are created, populated, and validated.
DO explicitly name views in action methods.
After you have setup the context in the action method for HTML generation, you will return a ViewResult or aPartialViewResult object. If you do not pass a view name to the result class, the view file will be chosen based upon the receiving action name. For example, given a controller named Products with an action named List. You can call “return View()
” from within the List action method without any parameters. The framework will look for a view called /Views/Products/List.aspx. If that view is missing, it will try /Views/Products/List.ascx. If that is not present, it tries /Views/Shared/List.aspx and then /Views/Shared/List.ascx. So, you can use /Views/Shared for any views that are shared across multiple controllers.
To avoid confusion, explicitly name the view, such as "return View("explicitViewName")", in the action method. This allows you to call List from a different action, without the framework looking for a different view.
DO use Post/Redirect/Get (PRG) when submitting forms.
According to the definition of the HTTP POST and GET verbs:
- HTTP GET is used for non-changing (idempotent) data to your model.
- HTTP POST is used for changing data to your model.
Given this clear delineation, when receiving form data in your post back action method, return RedirectToAction(<actionName>), which will result in a HTTP 302 (temporary redirect) and will generate a GET on the <actionName>. This results in Post-Redirect-Get pattern.
Therefore, do not use HTTP GET for posting form data, as this is a violation of the purpose of HTTP GET verb.
Additionally, classic ASP.NET postbacks can be the cause of a problematic feedback loop when posting forms.
For example, in the diagram below using a standard postback, you do a GET and POST against the same url (create.aspx). This is a problem when a user of the site gets impatient waiting on the form post to complete. If they hit the browser refresh button, there is the potential of submitting duplicate data which your web site will have to handle. You can solve this problem in MVC using the Post-Redirect-Get pattern.
However, this pattern does come with client side performance penalty, because the redirect causes further requests to the server. This performance cost has to weighed against the usability benefits of this pattern during the decision making process.
DO implement HandleUnknowAction and HandleError.
The default response for an unknown action is 404 (Not Found) error. If you override the HandleUnknownActionclass in a controller, you can implement a “default” view in this error. Additionally, put the HandleError attribute on an action and/or controller and you can provide a standard error view for when an uncaught exception is thrown.
Routing Recommendations
Routing is used in ASP.NET MVC to map URLs directly to a controller, rather than a specific file. This is especially useful for readability, as the developer can focus on designing URLs that are human readable (for example, product support and search engine indexing).
The default routes are added to a RouteTable which is located inside of Application_Start section of the Global.asax file. The table allows you to map a specific URL to a controller and action.
DO order routes from specific to general when using standard routing.
The route table is ordered, therefore create routes from most specific to general.
Consider the following example. Suppose you have a product catalog for which you would like to create URLs of the following forms:
- http://sellmyproducts/
- http://sellmyproducts/Page#
- http://sellmyproducts/category
- http://sellmyproducts/category/Page#
Given the following signature of the List method (on ProductsController class):
public ViewResult List(string category, int page)
The following routing specification will correctly route the user to the correct views, given the previously specified schema:
routes.MapRoute( null, "", new { controller = "Products", action = "List", category = (string)null, page = 1 } ); routes.MapRoute( null, "Page{page}", new { controller = "Products", action = "List", category = (string)null }, new { page = @"\d+" } ); routes.MapRoute( null, "{category}", new { controller = "Products", action = "List", page = 1} ); routes.MapRoute( null, "{category}/Page{page}", new { controller = "Products", action = "List"}, new { page = @"\d+" } );
DO use named route mechanism to avoid route ambiguity.
When you rely upon the ASP.NET routing mechanisms, you must know how the routing mechanisms work. Otherwise, you can create a lot of extra work tracking down misbehaving routes. One way to mitigate this problem is to explicitly name the routes.
For example, the following route mapping define named routes:
routes.MapRoute( "Default", "", new { controller = "Products", action = "List", category = (string)null, page = 1 } ); routes.MapRoute( "PageRoute", "Page{page}", new { controller = "Products", action = "List", category = (string)null }, new { page = @"\d+" } );
Using these route definitions, you can create a link which resolves to "PageRoute" as follows:
<%= Html.RouteLink("Next", "PageRoute", new RouteValueDictionary( new { page = i + 1 } )); %>
Extensibility Recommendations
Inside of the ASP.NET MVC framework there are many points for extension. You can replace any of the core components, a partial list of which includes the following:
- routing engine (MvcRouteHandler)
- controller factory (IControllerFactory)
- view engine (IViewEngine)
For example, you might want to write your own controller factory that uses an inversion of control container.
Rewriting core components is outside the scope of this topic. However, you could extend the framework by adding custom behaviors in the form of filters. Some of the standard filters included in the framework are: OutputCache, HandleError, and Authorize.
DO use filters for adding behaviors
MVC has a mechanism to add behaviors (filters) through the use of attributes before and after action methods and action results. These filters allow for extensibility which is lightweight in the request handling pipeline.
Filters can be applied to a specific action method to alter its behavior. Alternatively, a filter can be applied to a controller class, in which case it will take effect on every action method in the controller class. Base classes can be used to define common behavior patterns by applying filters to the base controller class and then ensuring that other controllers derive from that base class.
For example, suppose that you want to add functionality to log information for each request to debug a problem with HTTP headers. The following code define class that derives from the ActionFilterAttribute class.
public class LogHeadersFilterAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { foreach (string header in filterContext.HttpContext.Request.Headers.AllKeys) { Debug.WriteLine("Header " + header); Debug.WriteLine("Value " + filterContext.HttpContext.Request.Headers.Get(header)); } base.OnActionExecuting(filterContext); } }
In order to add this filter to a given action method, you simply place the LogHeadersFilter attribute at the top of the action (or controller) that you want to filter.
Testability Recommendations
One of the major strengths of the MVC pattern is the improved testability of the designs by keeping concerns separated and decoupled. In ASP.NET MVC you can cleanly separate and test the business logic in the model. For example, you could test the logic for adding a bid to the auction site without depending upon either the controller or the view.
DO write units tests.
ASP.NET MVC provides many of the tools developers need to create testable applications. In addition, it is relatively easy add a third-party unit test framework, mocking framework, or dependency injection framework. It is beyond the scope of this topic to tell you how to create unit tests for your application. However, ASP.NET MVC provides a flexible architecture that allows for easy testing. Unit testing is easier because of features like pluggable view engines, controller factories, action result types, and wrappers around the ASP.NET context types. For more information about unit testing an ASP.NET MVC application, see Unit Testing in MVC Applications.
Security Recommendations
Security is an important aspect of any modern software development project. Although no framework can provide perfect security, there is much you can do to help safeguard your ASP.NET MVC application.
DO guard against common attack vectors.
Website security needs to concern all web developers writing enterprise class websites and services. There are a host of well known attack vectors that you should know about. These attack vectors include (but are not limited to):
- Cross-site scripting (XSS) attacks
- SQL injection
- Cross-site Request Forgery (XSRF)
- Improperly implementing model binding
To prevent cross-site scripting (XSS) attacks:
- Disable request validation through use of the ValidateInput attribute. This attribute will falsely reject valid HTML input.
- Add Html.Encode for all user input data that is displayed, whether immediately rendered or the data is put into the database and then displayed later.
- Set the HttpOnly flag on cookies. This will prevent JavaScript from reading and sending cookies.
To prevent SQL injection:
- Always use parameterized SQL queries.
- Do not pass raw SQL to the database.
- Use an object-relational mapper (ORM) such as Entity Framework, which can completely eliminate the need to have SQL statements in the application code.
To prevent cross-site request forgery (XSRF):
- Use the Html.AntiForgeryToken class in forms to protect against cross-site forgery request. On the action which takes the post request, place the ValidateAntiForgeryToken attribute
To properly implement model binding:
- Explicitly call out with which members of a class you're model binding to, use the [Bind(Include=explicit members here)] attribute above the class declaration. For example, if there is an IsAdmin field on a class, automatic binding without Bind directives would allow it to bind to this class.
- Alternatively, create so-called view models, which are model objects specifically designed to accept input from external sources. View models contain only properties that are safe for external input. After model binding has created, populated, and validated these objects, the property values of these objects can be mapped to the application model or data model for further processing.
DO authenticate and authorize users to protect content.
It is beyond the scope of these guidelines to provide a deep treatment of authentication and authorization. However, you must annotate restricted data views, through either writing your own RoleProvider, or judicious use of the Authorize filter attribute.
DO use <%: %> (.NET 4) to protect against XSS attacks.
Prior to .NET 4.0 the developer would have to ensure there HTML was encoded by using code like the following:
<%= Html.Encode(ViewData["name"]) %>
This code was need to protect against XSS (cross-site scripting) attacks.
If you are using .NET 4, do not use the above syntax. Use the following syntax instead.
<%: ViewData["name"] %>
This new syntax automatically HTML encodes the string (if necessary), and is preferred.
Localization and Globalization Recommendations
Globalization is the process of making a product multi-lingual, where localization is the process of adapting a global product for a particular language and country. To develop a web application that supports globalization and localization, keep at least one rule in mind. Do not use hard-code strings in views.
DO use ASP.NET special resource folders and resource files.
While writing your web pages add an ASP.NET project folder for globalized content (App_GlobalResources) and for localized content for a given view (App_LocalResources). In each of these folders, you should add a resource (.resx) file that you should name according to the controller name. In other words, if your controller is named SubmissionPipeline, the resource file should be named SubmissionPipeline.resx.
Visual Studio converts this text mapping class into a global class that you can call using the following syntax:
Resources.<resource_filename>.<string_name>
Then you would access the resource in the view like this:
<%= Resources.SubmissionPipeline.continueButton %>
When the translated resource files become available, you should name each file using the following format: <filename>.<language>.resx.
For example, the German version of the resource file would be named: SubmissionPipeline.de.resx.
Performance Recommendations
Performance is a multi-faceted problem for web-sites, as a myriad of bottlenecks can affect performance including:
- Database
- Inefficient queries
- Incorrectly placed indexes
- Non-normalized design
- Bandwidth problems
- Large request size (affected by individual large images, .css, .js, .html, etc.)
- Content that references many other items, such as multiple script, CSS, or image files
- Slow connection speed
- Processing power
- Server: expensive operations
- Client: poorly written javascript
This section will focus solely on server processing and request size.
DO consider partial page updates using AJAX for reducing bandwidth.
One way to mitigate performance issues involving both server processing and request size is to use asynchronous Javascript (AJAX) to do partial page updates. ASP.NET MVC has built-in AJAX support, which helps to facilitates this pattern. The performance benefit occurs because the pattern reduces the amount of processing that the server must do to render a request and reduce the size of the HTML fragment.
The following example explains how to to partial page updates using AJAX:
- Choose what portion of HTML you want to update, and mark it with an ID.
<div id="items"> to be updated dynamically </div>
- Add the Javascript files that enable AJAX to the HTML (typically in the master view):
<script src="<%= Url.Content("~/Scripts/MicrosoftAjax.js") %>" type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/MicrosoftMvcAjax.js") %>" type="text/javascript"></script>
- In the view you want to update, add an AJAX link that references an action method (in the example the action method is named RetrieveItems).
<%= Ajax.ActionLink("Refresh All", "RetrieveItems", new { criteria = "all" } , new AjaxOptions { UpdateTargetId = "items" })%>
- Implement the controller action, and have it return the partial view.
DON'T overuse session, instead use TempData for short lived (intra-request) storage.
It is tempting when creating websites to add objects to the Session object, so that they are readily available. The problem with placing these objects in the Session object is that it can bog down the server by having to store extra information that may only be necessary across a redirect. The correct way to store these temporary variables across a redirect is by using the TempData dictionary.
For example, suppose you receive form data from a POST at login. The action method that procedure the POST might be something like the following:
[AcceptVerbs(HttpVerbs.Post)] public ActionResult LogIn(Seller person) { ... TempData["name"] = person.Name; return RedirectToAction("ItemUpload"); }
In this example, before redirecting to the ItemUpload action, the name of the seller is placed in the TempData dictionary. In the ItemUpload action method, the seller name is retrieved from the TempData dictionary and placed in the ViewData dictionary so it can be referenced in the view.
public ActionResult ItemUpload() { string name = TempData["name"] as string; ViewData["name"] = name; return View(); }
DO use an OutputCache filter for static pages.
Use OutputCache attribute when you are returning less frequently updated data; a good candidate may be your home page. You can use this technique for both HTML and JSON data types. When using it, only specify the cache profile name; do not specify anything else. If you need to fine tune caching, use the output cache section of the Web.config file.
For example, the OutputCache attribute is attached to Dashboard action method in the following code.
[AcceptVerbs(HttpVerbs.Get), OutputCache(CacheProfile = "Dashboard")] public ActionResult Dashboard(string userName, StoryListTab tab, OrderBy orderBy, int? page) { ... }
In the Web.config file, the duration is fine tuned to 15 seconds.
DO consider using asynchronous controllers for long running requests.
ASP.NET’s threading pool has a default limit of 12 concurrent worker threads per CPU. When the requests overload the server’s ability to process these requests, a queue is built up of requests. For example, any request which takes a considerable amount of time waiting for external resources, such as database or large file operations. These external requests block the thread they occupy for the entire wait period. When this queue gets too large (5000 requests pending), the server starts responding with 503 (server too busy) errors.
In ASP.NET 4 the number of concurrent threads is set by default to 5000. While it is possible to increase the default limits, there is a better way to mitigate long running requests from tying up threads, modifying the long running requests to run asynchronously. ASP.NET MVC enables you to implement asynchronous controllers for this purpose. For more information about how to implement an asynchronous controller, see Using an Asynchronous Controller in ASP.NET MVC.
【转】ASP.NET MVC 的最佳实践的更多相关文章
- [转]ASP.NET MVC 4 最佳实践宝典
原文:http://www.cnblogs.com/sonykings/archive/2013/05/30/3107531.html ASP.NET MVC最佳实践 本文档提供了一套旨在帮助创建最佳 ...
- 【转】.NET(C#):浅谈程序集清单资源和RESX资源 关于单元测试的思考--Asp.Net Core单元测试最佳实践 封装自己的dapper lambda扩展-设计篇 编写自己的dapper lambda扩展-使用篇 正确理解CAP定理 Quartz.NET的使用(附源码) 整理自己的.net工具库 GC的前世与今生 Visual Studio Package 插件开发之自动生
[转].NET(C#):浅谈程序集清单资源和RESX资源 目录 程序集清单资源 RESX资源文件 使用ResourceReader和ResourceSet解析二进制资源文件 使用ResourceM ...
- Java Servlet开发的轻量级MVC框架最佳实践
在Servlet开发的工程实践中,为了减少过多的业务Servlet编写,会采用构建公共Servlet的方式,通过反射来搭建轻量级的MVC框架,从而加快应用开发. 关于Servlet开发的基础知识,请看 ...
- Asp.Net MVC大型项目实践整合 NHibernate与Json序列化
通过NHibernate我们多表查询是实现了 但由于查询出来的集合中的对象“不是平的”,如何在送到UI绑定成了问题.ExtJs UI组件的数据绑定支持多种格式,如简单数组,Json,Xml.在本项目中 ...
- 关于单元测试的思考--Asp.Net Core单元测试最佳实践
在我们码字过程中,单元测试是必不可少的.但在从业过程中,很多开发者却对单元测试望而却步.有些时候并不是不想写,而是常常会碰到下面这些问题,让开发者放下了码字的脚步: 这个类初始数据太麻烦,你看:new ...
- 【转】ASP.NET MVC教程
转自:http://www.cnblogs.com/QLeelulu/category/123326.html ASP.NET MVC的最佳实践与性能优化的文章 摘要: 就一些文章链接,就不多废话了. ...
- ASP.NET跨平台最佳实践
前言 八年的坚持敌不过领导的固执,最终还是不得不阔别已经成为我第二语言的C#,转战Java阵营.有过短暂的失落和迷茫,但技术转型真的没有想象中那么难.回头审视,其实单从语言本身来看,C#确实比Java ...
- 【jqGrid for ASP.NET MVC Documentation】.学习笔记.4.性能
1 HTML / ViewState 大小 1.1 HTML 大小 jqGrid for ASP.NET MVC 使用最佳的客户端渲染,意味着 HTML gird 的 尺寸是最小的.事实上,只有 gr ...
- ASP.NET MVC防范CSRF最佳实践
XSS与CSRF 哈哈,有点标题党,但我保证这篇文章跟别的不太一样. 我认为,网站安全的基础有三块: 防范中间人攻击 防范XSS 防范CSRF 注意,我讲的是基础,如果更高级点的话可以考虑防范机器人刷 ...
随机推荐
- URI与URL区别
URL 与 URI 很多人会混淆这两个名词. URL:(Uniform/Universal Resource Locator 的缩写,统一资源定位符). URI:(Uniform Resource I ...
- Windows上的文件合并命令
从Linux转到Windowns后,发现很多好用的shell命令都没有了,但实际情况是Windows一样有DOS时代的命令窗口,在CLI年代用DOS的人也要干活. 比如,今天想将几个单独的sql文件整 ...
- error: failed to initialize alpm library
这个问题出在archlinux上面 [root@sarch pacman]# pacman -Syuerror: failed to initialize alpm library(database ...
- PCV 学习笔记-ch1 主成分分析实现
模块名称:pca.py PCA原理与紧致技巧原理待补... #-*-coding:UTF-8-*- ''' Created on 2015年3月2日 @author: Ayumi Phoenix ch ...
- window.opener用法
[转]window.opener用法 window.opener 实际上就是通过window.open打开的窗体的父窗体. 比如在父窗体parentForm里面 通过 window.open(&quo ...
- C++ Primer:第七章:类
定义一个类: class Myclass{ int data_i; string data_str; public: int getdata_i() const { return data_i; } ...
- DIV的圆角表现和TAB切换
内容大体是从网上找过来的,感觉那位哥们说的对,css还是比较靠谱的,当然有些高玩搞出来的东西本地 没有运行起来. 把自己的应用贴出来同大家分享 <html> <head> &l ...
- c#部分---需要实例化的内容;
需要初始化的: 随机数类:初始化 实例化//不允许将初始化的语句放置在循环中//Random ran = new Random(); 时间值类:/初始化 实例化//DateTime dt = new ...
- 【HAOI2009】【P1307】毛毛虫
感觉相比其他树归题简单多了,不过有点绕(也许是我的思路很奇怪一.一)(这是省选题啊,就算作为T1这题也太水了,HA好弱……) 原题: 对于一棵树,我们可以将某条链和与该链相连的边抽出来,看上去就象成一 ...
- Java设计模式之适配器设计模式
1.适配器模式( Adapter)定义将一个类的接口转换成客户希望的另外一个接口.Adapter 模式使得原来由于接口不兼容而不能一起工作的 那些类可以一起工作. 现实案例如下: 墙上电源类(22 ...