1、自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中!即:创建一个实现了IHttpmodule接口的类,并将配置WebConfig。
   在自定义的HttpModule中,可以将一个方法注册到HttpApplication的任意一个事件中,在之后执行HttpApplication一些列事件时,按照事件的顺序(事件又按照添加方法先后的顺序)执行注册在事件中的方法!

namespace MvcStore.Models
{
    public class ExcuteHttpRequestModule:IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.PostResolveRequestCache+=new EventHandler(this.context_ExecuteHttpRequst);
           
        }
        public void Dispose()
        {
           
        }

        public void context_ExecuteHttpRequst(object sender, EventArgs e)
        {
            HttpRequest httpRequest = HttpContext.Current.Request;
            Uri previousUri = httpRequest.UrlReferrer;
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=152368
  -->

<configuration>
  <appSettings>
    <add key="webpages:Version" value="1.0.0.0"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
  </appSettings>
    
  <system.web>
    <!--自定义HttpModule,仅添加一下此段代码即可-->
    <httpModules>
      <add name="ExecuteHttpRequestModule" type="MvcStore.Models.ExcuteHttpRequestModule"/>
    </httpModules>
    
     ......等
</configuration>

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

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

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

2、添加路由规则

 routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );

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;
        }
    }
}
微软定义的MapRoute方法

相关文章: