代码如下
public class RouteHandler:IRouteHandler
{
public string virtualPath { get; private set; }
public RouteHandler(string virtualPath)
{
this.virtualPath = virtualPath;
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var originalPage = BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page)) as IHttpHandler;
if (originalPage != null)
{
var routePage = originalPage as IRouteTablePage;
if (routePage != null)
{
routePage.RequestContext = requestContext;
}
}
return originalPage;
}
}
public class RoutePage:Page,IRouteTablePage
{
#region IRouteTablePage 成员
public RequestContext RequestContext
{
get;
set;
}
#endregion
public RouteValueDictionary RouteValues
{
get
{
if (RequestContext != null)
{
return RequestContext.RouteData.Values;
}
return null;
}
}
public object GetRouteValue(string key)
{
object resultvalue = null;
if (RouteValues != null && RouteValues.Count > 0)
{
RouteValues.TryGetValue(key,out resultvalue);
}
return resultvalue;
}
}
interface IRouteTablePage
{
RequestContext RequestContext { get;set;}
}
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes); //RouteTable.Routes就是路由规则的集合,RouteTable就是系统定义的全局的静态路由表了
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
public void RegisterRoutes(RouteCollection routes)
{
routes.Add(new Route("news/category/{id}", new RouteHandler("Default.aspx")));
routes.Add(new Route("news/{id}/{page}", new RouteHandler("Default.aspx")));
routes.Add(new Route("news/{year}/{month}/{day}", new RouteHandler("Default.aspx")));
routes.Add(new Route("search", new RouteHandler("Default.aspx")));
}
}
就是找不到Default页面报如下错误
是路由写错了