Page.GetRoutUrl in a static method

Some information on what I'm doing

I usually like it when my pages return the URL needed to access it. So I will usually have this method

public partial class ProductDetails : Page
{
    public static string GetUrl(Guid productId)
    {
        return "fully qualified url";
    }
}

on my other pages / controls that need to access this page, I just set the link as

hl.NavigateUrl = ProductDetails.GetUrl(id);

I am working with new UrlRouting stuff in 4.0 and have come across something that I'm not sure if this will work. I am trying to use Page.GetRouteUrl in my static method, and obviously it explodes because the page is not static.

Does anyone know if it is possible to repeat what I am doing with GetRouteUrl?

THX

+3
source share
2 answers

- :

var url = ((Page)HttpContext.Current.Handler).GetRouteUrl(id);

. , , - ... , , .

+3

GetRouteUrl Nicks .

GetRouteUrl. , GetVirtualPath

public static string GetUrl(int productId)
{
    var parameters = new RouteValueDictionary { { "productId", productId } };
    var vpd = RouteTable.Routes.GetVirtualPath(null, "product-details", parameters);
    return vpd.VirtualPath;
} 
+2

Source: https://habr.com/ru/post/1731253/


All Articles