【问题标题】:Construct URLs from Routes asp.net web form 4.0从 Routes asp.net web form 4.0 构建 URL
【发布时间】:2012-01-05 00:25:13
【问题描述】:

我正在使用 asp.net 4.0 网络表单项目和路由。我需要知道处理请求的实际页面。假设我的路由 url 是这样的

http://mysite.com/product/audi 基本上我有 product.aspx 页面,并且 audi 作为查询字符串值传递。实际上 product.aspx 正在处理请求。我可以以编程方式提取页面名称和查询字符串名称和数据吗?因此,我可以稍后构建 url,例如

http://mysite.com/product.aspx?cat=audi

请帮助我使用路由 url 中的查询字符串构建 url。 谢谢

添加部分

当我们像这样在 global.asax 中映射路线时

`protected void Application_Start(object sender, EventArgs e)
{ 
 RegisterRoutes(RouteTable.Routes); 
}
public static void RegisterRoutes(RouteCollection routes)
{ 
 routes.MapPageRoute("","Product/{category}","~/Product.aspx");
}

然后我们得到类似http://www.mysite.com/products/software 的路由路径,但内部url 看起来像http://www.mysite.com/products.aspx?category=software。所以我只需要反转。我会将http://www.mysite.com/products/software 之类的 url 传递给任何例程,并且例程会像http://www.mysite.com/products.aspx?category=software 一样将 url 返回给我。告诉我我该怎么做。

【问题讨论】:

标签: asp.net asp.net-routing


【解决方案1】:

来自 ScottGu 的博客:

在 ASP.NET Web 表单中检索 URL

ASP.NET 中的 URL 路由引擎既可用于将传入 URL 映射到代码处理程序,也可用于使用相同的映射注册逻辑以编程方式生成传出 URL。

例如,上面当我们映射 /products/{category} URL 时,我们给它一个“友好名称”“products-browse”。这使我们现在还可以使用新的 Page.GetRouteUrl() 辅助方法在 URL 路由系统中查找路由,可选地为其指定参数,然后检索它映射回的实际 URL。例如,以下代码将检索“/products/software”的 URL 值:

URL Routing with ASP.NET 4 Web Forms (VS 2010 and .NET 4.0 Series)

【讨论】:

  • 我希望你没有理解我的问题。我更新了我的问题,如果您有任何问题,请查看并提供解决方案。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-30
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多