16 ASP. net mvc extensions [Source Code]

Source: Internet
Author: User

1. customize an HttpModule and add the methods to the corresponding event of HttpApplication! That is, create a class that implements the IHttpmodule interface and configure WebConfig. In the custom HttpModule, you can register a method to any event of HttpApplication. When you execute some columns of HttpApplication, execute the method registered in the event according to the Event order (the event is in the order of adding methods! + View Code example: Create an HttpModule (implementing the IHttpModule Interface) and register a method to the BeginRequest (first event of HttpAppliaction) event of HttpApplication, that is: because the method is registered in the first event of HttpApplication, the method will be executed for all request addresses, whether valid or invalid. Use HttpModule to expand knowledge and use NLog To Write Request logs. Download source code. Supplement: in ASP. net mvc, css and js requests are merged and sent to the server! 2. Add routing rules + View Code3. in step 1 of the custom MapRoute method, the MapRoute method is actually an extension method of RouteCollection. We can also define one. Copy the namespace System code. web. mvc {public static class RouteCollectionExtensions {public static Route MapRoute (this RouteCollection routes, string name, string url) {return routes. mapRoute (name, url, null, null);} public static Route MapRoute (this RouteCollection routes, string name, string url, object defaults) {return routes. mapRoute (name, url, defaults, null);} public static Route MapRoute (this RouteCollection routes, string name, string url, object ults, object constraints) {return routes. mapRoute (name, url, defaults, constraints, null);} public static Route MapRoute (this RouteCollection routes, string name, string url, string [] namespaces) {return routes. mapRoute (name, url, null, null, namespaces);} public static Route MapRoute (this RouteCollection routes, string name, string Url, object ults, string [] namespaces) {return routes. mapRoute (name, url, defaults, null, namespaces);} public static Route MapRoute (this RouteCollection routes, string name, string url, object defaults, object constraints, string [] namespaces) {if (routes = null) {throw new ArgumentNullException ("routes");} if (url = null) {throw new ArgumentNullException ("url ");} route route = new R Oute (url, new Dictionary () {Defaults = new RouteValueDictionary (defaults), Constraints = new RouteValueDictionary (constraints), DataTokens = new RouteValueDictionary ()}; if (namespaces! = Null & namespaces. length> 0) {route. dataTokens ["Namespaces"] = namespaces;} routes. add (name, route); return route ;}} copy the code and copy the namespace MvcExtension code. models {public static class MyRouteCollectionExtensions {// <summary> // customize the MapRoute method // </summary> /// <param name = "routes"> </ param> /// <param name = "routeHandler"> </param> /// <param name = "name"> </param> /// <param name =" url "> </param> // /<Param name = "defaults"> </param> // <param name = "constraints"> </param> // <param name = "namespaces"> </param> // <returns> </returns> public static Route MyMapRoute (this RouteCollection routes, IRouteHandler routeHandler, string name, string url, object ults, object constraints, string [] namespaces) {if (routes = null) {throw new ArgumentNullException ("routes ");} if (url = null) {throw New ArgumentNullException ("url");} if (routeHandler = null) {throw new ArgumentNullException ("routeHandler");} Route route = new Route (url, routeHandler) {Defaults = new RouteValueDictionary (defaults), Constraints = new RouteValueDictionary (constraints), DataTokens = new RouteValueDictionary ()}; if (namespaces! = Null & namespaces. length> 0) {route. dataTokens ["Namespaces"] = namespaces;} routes. add (name, route); return route ;} /// <summary> /// custom MapRoute method // </summary> /// <param name = "routes"> </param> // <param name = "name"> </param> // <param name = "route"> </param> // <returns> </returns> public static Route MyMapRoute (this RouteCollection routes, string name, Route route) {if (routes = null) {Throw new ArgumentNullException ("routes");} if (route = null) {throw new ArgumentNullException ("route");} routes. add (name, route); return route ;}} copy the code. Note: The MapRoute method provided by Microsoft shows that when creating a Route object, its constructor parameters include: new MvcRouteHandler. This MvcRouteHandler is used to create an HttpHandler object. HttpHandler is used to process the request! 4. Custom MvcRouteHandler: implements the IRouteHandler interface. By default, MVC uses MvcRouteHandler to create an HttpHandler object to process requests! Copy the namespace System code. web. mvc {public class MvcRouteHandler: IRouteHandler {private IControllerFactory _ controllerFactory; public MvcRouteHandler () {} public MvcRouteHandler (IControllerFactory controllerFactory) {this. _ controllerFactory = controllerFactory;} protected virtual IHttpHandler GetHttpHandler (RequestContext requestContext) {requestContext. httpContext. setSessionStateBehavior (this. ge TSessionStateBehavior (requestContext); return new MvcHandler (requestContext);} protected virtual SessionStateBehavior GetSessionStateBehavior (RequestContext requestContext) {string controllerName = (string) requestContext. routeData. values ["controller"]; IControllerFactory controllerFactory = this. _ controllerFactory ?? ControllerBuilder. current. getControllerFactory (); return controllerFactory. getControllerSessionBehavior (requestContext, controllerName);} IHttpHandler IRouteHandler. getHttpHandler (RequestContext requestContext) {return this. getHttpHandler (requestContext) ;}} copy the Code definition: we only need to implement the IRouteHandler interface when customizing MvcRouteHandler. For specific implementation, refer to the MvcRouteHandler class and View Code defined by Microsoft: add your own MvcRouteHandler object In the Route object!

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.