且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

asp.net 4.0 Web窗体路由 - 默认/通配符路线

更新时间:2023-02-15 21:10:37

您可以匹配这样所有剩余的路线:

You can match all remaining routes like this:

routes.MapPageRoute("defaultRoute", "{*value}", "~/Missing.aspx");

在这种情况下,我们知道所有路由,并想送什么别的了失踪/ 404页。只要确保把这个作为过去的路线,因为它是一个通配符,将捕获的一切。

In this case, we know all routes, and want to send anything else to a "missing"/404 page. Just be sure to put this as the last route, since it is a wildcard and will catch everything.

另外,您可以注册一个路线以同样的方式,但是在内部做映射到一个页面,如下所示:

Alternatively you could register a route the same way, but internally does mapping to a page, like this:

routes.Add(new Route("{*value}", new DefaultRouteHandler()));

这是处理类会做你的通配符映射,是这样的:

That handler class would do your wildcard mapping, something like this:

public class DefaultRouteHandler : IRouteHandler
{
  public IHttpHandler GetHttpHandler(RequestContext requestContext)
  { 
    //Url mapping however you want here:
    var pageUrl = requestContext.RouteData.Route.Url + ".aspx";

    var page = BuildManager.CreateInstanceFromVirtualPath(pageUrl, typeof(Page)) 
               as IHttpHandler;
    if (page != null)
    {
      //Set the <form>'s postback url to the route
      var webForm = page as Page;
      if (webForm != null) 
         webForm.Load += delegate { webForm.Form.Action = 
                                    requestContext.HttpContext.Request.RawUrl; };
    }
    return page;
  }
}

这是打破奇数地方prevent水平滚动了一下,但你得到的全局观念。再次,确保这是在最后的路线,否则它会处理的所有的你的路由。

This is broken a bit in odd places to prevent horizontal scrolling, but you get the overall point. Again, make sure this is the last route, otherwise it'll handle all your routes.