1 namespace System.Web.Routing {
2     using System;
3     using System.Diagnostics.CodeAnalysis;
4     using System.Globalization;
5     using System.Web;
6     using System.Runtime.CompilerServices;
7 
8     [TypeForwardedFrom("System.Web.Routing, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")]
9     public abstract class UrlRoutingHandler : IHttpHandler {
10 
11         private RouteCollection _routeCollection;
12 
13         protected virtual bool IsReusable {
14             get {
15                 return false;
16             }
17         }
18 
19         [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly",
20             Justification = "This needs to be settable for unit tests.")]
21         public RouteCollection RouteCollection {
22             get {
23                 if (_routeCollection == null) {
24                     _routeCollection = RouteTable.Routes;
25                 }
26                 return _routeCollection;
27             }
28             set {
29                 _routeCollection = value;
30             }
31         }
32 
ProcessRequest(HttpContext httpContext)33         protected virtual void ProcessRequest(HttpContext httpContext) {
34             ProcessRequest(new HttpContextWrapper(httpContext));
35         }
36 
ProcessRequest(HttpContextBase httpContext)37         protected virtual void ProcessRequest(HttpContextBase httpContext) {
38             RouteData routeData = RouteCollection.GetRouteData(httpContext);
39             if (routeData == null) {
40                 throw new HttpException(404, SR.GetString(SR.UrlRoutingHandler_NoRouteMatches));
41             }
42 
43             IRouteHandler routeHandler = routeData.RouteHandler;
44             if (routeHandler == null) {
45                 throw new InvalidOperationException(SR.GetString(SR.UrlRoutingModule_NoRouteHandler));
46             }
47 
48             RequestContext requestContext = new RequestContext(httpContext, routeData);
49             IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext);
50             if (httpHandler == null) {
51                 throw new InvalidOperationException(
52                     String.Format(
53                         CultureInfo.CurrentUICulture,
54                         SR.GetString(SR.UrlRoutingModule_NoHttpHandler),
55                         routeHandler.GetType()));
56             }
57 
58             VerifyAndProcessRequest(httpHandler, httpContext);
59         }
60 
VerifyAndProcessRequest(IHttpHandler httpHandler, HttpContextBase httpContext)61         protected abstract void VerifyAndProcessRequest(IHttpHandler httpHandler, HttpContextBase httpContext);
62 
63         #region IHttpHandler Members
64         bool IHttpHandler.IsReusable {
65             get {
66                 return IsReusable;
67             }
68         }
69 
IHttpHandler.ProcessRequest(HttpContext context)70         void IHttpHandler.ProcessRequest(HttpContext context) {
71             ProcessRequest(context);
72         }
73         #endregion
74     }
75 }
76