asp.net 4.0 Web窗体路由 - 默认/通配符路线 [英] asp.net 4.0 web forms routing - default/wildcard route

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

问题描述

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

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 /文件夹2 /页。 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页。只要确保把这个作为过去的路线,因为它是一个通配符,将捕获的一切。

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.

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

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