生成URL路由的Web表单的URL [英] Generate a URL with URL Routing in Webforms
问题描述
我知道在MVC框架,你的HTML类来创建网址:
I know in the MVC Framework, you have the Html Class to create URLs:
Html.ActionLink("About us", "about", "home");
但是,如果你想在Web表单生成URL是什么?
But what if you want to generate Urls in Webforms?
我还没有找到上生成与Web表单的URL的细节一个很好的资源
I haven't found a really good resource on the details on generating URLs with Webforms.
例如,如果我产生像这样的路线:
For example, if I'm generating routes like so:
Route r = new Route("{country}/{lang}/articles/{id}/{title}",
new ArticleRouteHandler("~/Forms/Article.aspx"));
Route r2 = new Route("{country}/{lang}/articles/",
new ArticleRouteHandler("~/Forms/ArticlesList.aspx"));
Routes.Add(r);
Routes.Add(r2);
我将如何产生使用路由表中的数据的URL。
How would i generate URLs using the Routing table data.
如。 / CA / EN /用品/ 123 /无
eg. /ca/en/articles/123/Article-Title without
推荐答案
至于你说的文章,标题,ASP.NET MVC为您提供了一组的辅助方法来反向查找的RouteTable,并为你生成一个URL。我没有这个出场不多但但据我可以看到你需要调用一个RouteCollection(最有可能RouteTable.Routes)的GetVirtualPath方法。因此,像:
As you say, ASP.NET MVC offers you a set of helper methods to "reverse lookup" the RouteTable and generate a URL for you. I've not played with this much yet but as far as I can see you need to call the GetVirtualPath method on a RouteCollection (most likely RouteTable.Routes). So something like:
Dim routedurl = RouteTable.Routes.GetVirtualPath(context, rvd).VirtualPath
您需要传递的RequestContext和RouteValueDictionary。该RouteValueDictionary包含路由参数(所以你的情况类似县=英国,LANG =EN-GB等最棘手的部分是RequestContext的,因为这是不正常的的HttpContext的一部分。你可以将其推入HttpContext的在你的IRouteHandler:
You need to pass the RequestContext and a RouteValueDictionary. The RouteValueDictionary contains the route parameters (so in your case something like county="UK", lang="EN-GB" etc. The tricky part is the RequestContext as this is not part of the normal HttpContext. You can push it into the HttpContext in your IRouteHandler:
requestContext.HttpContext.Items("RequestContext") = requestContext
,然后在你的IHttpHandler(aspx页面)重新恢复它在必要时:
and then restore it again in your IHttpHandler (aspx page) when required:
Dim rvd =
New RouteValueDictionary(New With {.country = "UK", .lang = "EN-GB"})
Dim routedurl =
RouteTable.Routes.GetVirtualPath(context.Items("RequestContext"), rvd).VirtualPath
道歉应对VB中的一个C#的问题,它只是使ASP.NET路由网站我只好用手在VB.NET。
Apologies for responding to a C# question in VB, it was just that the ASP.NET routing site I had to hand was in VB.NET.
这篇关于生成URL路由的Web表单的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!