16个ASP.NET MVC扩展点[附源码]

发布时间:2017-09-09 10:52:29
16个ASP.NET MVC扩展点[附源码] 1、自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中!即:创建一个实现了IHttpmodule接口的类,并将配置WebConfig。

  在自定义的HttpModule中,可以将一个方法注册到HttpApplication的任意一个事件中,在之后执行HttpApplication一些列事件时,按照事件的顺序(事件又按照添加方法先后的顺序)执行注册在事件中的方法!

+ View Code

+ View Code

例:创建一个HttpModule(实现IHttpModule接口),并将一个方法注册到HttpApplication的BeginRequest(HttpAppliaction的第一个事件)事件中,即:由于该方法注册在HttpApplication第一个事件中,专题,所有无论是合法还是非法的请求地址,该方法都会被执行。

利用HttpModule扩展知识,并通过NLog来完成写请求日志:源码下载

补充:在ASP.NET MVC中,css和js的请求是合并到一起发送给服务端的!

2、添加路由规则

+ View Code

3、自定义MapRoute方法

  第一步中MapRoute方法其实就是RouteCollection的扩展方法,我们也可以定义一个。

复制代码

namespace System.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 defaults, 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 defaults, 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 Route(url, new MvcRouteHandler())

{

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;

}

}

}

复制代码

复制代码

namespace MvcExtension.Models

{

public static class MyRouteCollectionExtensions

{

/// <summary>

/// 自定义MapRoute方法

/// </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 defaults, 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>

/// 自定义MapRoute方法

/// </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;

}

}

}

复制代码

注:在微软提供的MapRoute方法中可以看出,创建Route对象时,其构造函数的参数中有:new MvcRouteHandler。这个MvcRouteHandler用于之后创建HttpHandler对象,HttpHandler就是用来最后处理请求的!

4、自定义MvcRouteHandler

  即:实现IRouteHandler接口,MVC默认使用MvcRouteHandler来创建HttpHandler对象,用来处理请求!

复制代码

namespace System.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.GetSessionStateBehavior(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);

}

}

}

复制代码

  定义:我们自定义MvcRouteHandler时只需实现IRouteHandler接口,具体实现参照微软定义的MvcRouteHandler类

+ View Code

  使用:结合2中创建的自定义的MapRoute方法,将自己的MvcRouteHandler对象添加到Route对象中!

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:襄阳网站建设 http://xiangyang.45qun.com