asp.net 4.0 网络表单路由 - 默认/通配符路由 [英] asp.net 4.0 web forms routing - default/wildcard route

查看:26
本文介绍了asp.net 4.0 网络表单路由 - 默认/通配符路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用带有 Web 窗体的 ASP.NET 4.0 路由来生成将充当某种通配符的路由时,我有一种简单的方法吗?

I there a simple way when using ASP.NET 4.0 routing with Web Forms to produce a route that will act as some kind of wildcard?

在我看来,在 WebForms 中,您必须为每个页面指定一个路由 - 我正在寻找某种通用路由,可以在不需要任何特定内容的情况下使用,也许直接从路径映射到路径,所以...

It seems to me that within WebForms, you have to specify a route for every page - I am looking for some kind of generic route that can be used where nothing specific is required, perhaps mapping directly from path to path so...

http://somedomain.com/folder1/folder2/page 可能会映射到文件夹1/folder2/page.aspx

http://somedomain.com/folder1/folder2/page would possibly map to folder1/folder2/page.aspx

有什么建议吗?

谢谢

推荐答案

你可以像这样匹配所有剩余的路由:

You can match all remaining routes like this:

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

在这种情况下,我们知道所有路由,并希望将其他任何内容发送到丢失"/404 页面.请务必将此作为 last 路由,因为它是一个通配符,可以捕获所有内容.

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

这在奇怪的地方被打破了一点,以防止水平滚动,但你明白了整体观点.再次确保这是最后一个路线,否则它将处理您的所有路线.

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.

这篇关于asp.net 4.0 网络表单路由 - 默认/通配符路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆