asp.net 4.0 Web 表单路由 - 默认/通配符路由

发布于 2024-08-30 20:10:20 字数 337 浏览 4 评论 0原文

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

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

http://somedomain.com/folder1/folder2/page 可能会映射到folder1/folder2/page。 aspx

有什么建议吗?

谢谢

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?

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 would possibly map to folder1/folder2/page.aspx

Any suggestions?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向日葵 2024-09-06 20:10:20

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

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

在这种情况下,我们知道所有路由,并且希望将其他任何内容发送到“丢失”/404 页面。请务必将其作为最后路线,因为它是通配符并且会捕获所有内容。

或者,您可以以相同的方式注册路线,但在内部映射到页面,如下所示:

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

该处理程序类将执行通配符映射,如下所示:

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

这在奇怪的地方有点破坏,以防止水平滚动,但您会得到总体要点。再次确保这是最后路线,否则它将处理您的所有路线。

You can match all remaining routes like this:

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

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.

夏有森光若流苏 2024-09-06 20:10:20

另外 - 请记住,如果您的 Web 应用程序中有验证控件,则需要在 Global.asax 文件中为 .axd 文件添加例外:

http://basgun.wordpress.com/2010/10/25/getting -syntax-error-in-asp-net-routing-due-to-webresource-axd/

否则,您将不断收到语法错误,因为路由选择 .axd 文件并且无法正确加载 JavaScript 文件验证控制所需的。

Additionally - Keep in mind that you need to add an exception for the .axd files in your Global.asax file if there are validation controls in your web app:

http://basgun.wordpress.com/2010/10/25/getting-syntax-error-in-asp-net-routing-due-to-webresource-axd/

Otherwise, you will keep getting a syntax error because the routing picks up the .axd files and not properly loads the JavaScript files needed for the validation controls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文