In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to understand ASP.NET MVC". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand ASP.NET MVC.
1.ASP.NET MVC request process
one
2.Controller
(1) the controller plays a role in handling client requests in ASP.NET MVC.
1) System.Web.Mvc.IController interface must be implemented
Usually inherits the System.Web.MVC.Controller class directly
2) it must end with Controller
3) handle specific client requests through different Action
3.Action
(1) means that types that inherit the return values defined in the System.Web.Mvc.Controller class are compatible.
(2) ActionResult method
The copy code is as follows:
Namespace MvcApplication.Controllers
{
Public class HomeController:Controller
{
Public ActionResult Index ()
{
ViewBag.Message= "Han Yinglong"
Return View ()
}
}
}
(3) ActionResult of ASP.NET MVC3
(4) points for attention
1) Action that can be accessed through URL must be a Public method
2) if the [NonAction] attribute is marked, the Action cannot be accessed through URL
3) by default, the method name obtained by Action is the Action name (the name accessed through URL). If you have special needs, you can also mark a specific Action name through [ActionName ("OtherActionName")].
4) We can use [HttpPost] [HttpGet] and so on to distinguish the Action of the same name that handles different request actions.
4.ASP.NET Routing routes, filter
(1) the responsibility of the ASP.NET Routing module (Module) is to map incoming client (browser) requests to a specific MVC Controller Actions
(2) routing mechanism
1) routing engine-Mapping URLS to Controlller
The copy code is as follows:
Public static void RegisterRoutes (RouteCollection routes)
{
Routes.IgnoreRoute ("{resource} .axd / {* pathInfo}")
Routes.MapRoute (
"Default", / / Route name
"{controller} / {action} / {id}", / / URL with parameters
New {controller = "Home", action = "Index", id = UrlParameter.Optional} / / default values for parameters
);
}
Protected void Application_Start ()
{
RegisterRoutes (RouteTable.Routes)
}
2) / Products/Detail/8
The copy code is as follows:
Routes.MapRoute (
"Default", / / Route name
"{controller} / {action} / {id}", / / URL with parameters
);
Public class ProductsController:Controller
{
Public ActionResult Details (int id)
{
Return View ()
}
Public ActionResult Edit (int id)
{
Return View ()
}
}
5. Routing changes in MVC3
(1) moved from the System.Web.Routing3.5 assembly to the System.Web 4 assembly and became part of the basic service.
(2) in ASP.NET 4, the Module of Routing is registered in the root Web.Config, so you do not need to register separately in the Web.Config in your own application.
(3) the number of events handled by UrlRoutingModule is reduced by one, and only PostResolveRequestCache events are handled.
(4) HttpRequest adds a new RequestContext attribute.
(5) added PageRouteHandler to support WebForm routing function
(6) the overload method of 4 MapPageRoute has been added to RouteCollection, which makes it more convenient to add routing rules.
6. Benefits of Routing
(1) it is convenient to implement REST service.
(2) Url is friendly, which is beneficial to SEO and enhance user experience.
(3) the calling rules of Controller and Action can be customized to reduce coupling and improve flexibility.
7. Filter
(1) Filter is a kind of AOP mode, which can interfere with a series of operations by crosscutting. It decouples the dependency to a great extent and makes our code more concise and more functional.
(2) four types of Filter interfaces are provided in ASP.NET MVC.
1) IActionFilter
2) IAuthorizationFilter
3) IExceptionFilter
4) IResultFilter
(3) OutputCacheAttribute,HandlErrorAttribute,AuthorizeAttribute and other common Filter implementations are provided in ASP.NET MVC.
(4) the cut-in process of Filter
1) take ActionFilter as an example
8. Filter in SP.NET MVC3
(1) Global registration Filter function is provided.
(2) OutputCache support for ChildAction is provided
1) use in combination with [ChildActionOnly]
9. Model
(1) the Model in MVC is mainly responsible for maintaining the data state, retrieving the data from the data memory and transferring it to the controller. The data transmitted by the client is processed and then transmitted back to the data storage system, which is a heavy layer in MVC.
(2) the ASP.NET MVC framework itself does not care about the data storage system, and simplifies the use of Model through some additional helper classes and Model binding mechanism.
1) self-binding mechanism
2) self-verification mechanism
(3) improvement of ASP.NET MVC3 Model
1) ASP.NET MVC3 Model mainly improves the verification mechanism.
-> data validation (Data Annotations)
-> client authentication (Client Validation)
-> remote authentication (Remote Validation)
-> self-verification (Self Validation)
(4) data verification
1) verify through the method set of System.ComponentModel.DataAnnotations, and have some convenient effects on client-side verification.
2) you can implement custom validated Attribute by inheriting ValidationAttribute
(5) client verification
1) use the authentication plug-in of Jquery
2) jquery.validate.unobtrusive.mis.js implements client-side verification
-> enable client authentication
-> reference JQuery
-> Special validation
@ {
Html.EnableClientValidation ()
}
(6) remote verification
1) it is similar to RequiredAttribute in Model.
1) [Remote ("authenticated Action name", "Controller name", ErrorMessage= "failed error message for remote validation")]
2) pay attention
1) the Action used for remote authentication must be HttpGet, and the Post submission is invalid
2) the result returned by Action is JsonResult, not a Boolean value directly
(7) self-verification
1) combine ValidationContext and ValidationResult in Model to provide verification
The copy code is as follows:
Public IEnumerable Validate (ValidationContext validationResult)
{
If (EndDate controls the server side
The cache will be counted (Output Caching)
Improve the speed of database query
2) ASP.NET MVC
-> call performance improvement
-> appropriate caching strategy
(5) [SeesionState]
1) use the SessionState attribute
Controls how Controller accesses phase state data (Session)
2) Note: after closing Session, you cannot use TempData to transmit information.
(6) [OutputCache]
1) Html.Action and Html.RenderAction support Output Caching
-> @ {Html.RenderAction ("ActionName")}
-> @ Html.Action ("ActionName")
2) ChildAction finally supports the OutputCache attribute
-> [ChildActionOnly]
Only Duration,VaryByCustom and VaryByParam parameters are supported
-> cannot use the CacheProfile parameter
(7) change the default settings of ViewEngine
1) remove excess ViewEngine to speed up the parsing of View
-> ViewEngines.Engines.Clear ()
-> ViewEngines.Engines.Add (new RazorViewEngine ())
2) you can also change the order in which View is loaded in this way
-> default is WebFormViewEngine priority
-> ViewEngines.Engines.Add (new WebFormViewEngine ())
(8) avoid intruding to the view for null (View)
1) Html.TextBoxFor (m = > m.Name)
-> Exception will be raised when null is passed, but will be dropped by try/catch
-> public ActionResultInsert () {
Return View (new Products ())
}
(9) disable debug mode of Web.Config
1)
At this point, I believe you have a deeper understanding of "how to understand ASP.NET MVC". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.