views:

105

answers:

1

There must me a more elegant way to build a URL with parameters in .NET then for example

Response.Write("<a href=HeadOfMarketView.aspx"+Session["HOM"] != null ? Session["HOM"]+">Head of Market</a> / ")

I mean the concatenation of strings is a litte bit old school, no?

A: 

Maybe this is better:

Response.Write(string.Format("<a href=HeadOfMarketView.aspx?param={0}>Head of Market</a>", Session["HOM"] as string ?? "" ));

EDIT: Response to the comment (C# 3.0, .NET 3.5):

Response.Write(string.Format("<a href=HeadOfMarketView.aspx{0}>Head of Market</a>", Session["HOM"].ToUrlParamString()));

public static class UrlHelper
{
    public static string ToUrlParamString(this object val)
    {
        return val != null ? "?" + val : string.Empty;
    }
}
Arthur
I need a solution where the parameter is only generated when the variable (in this case Session["HOM"]) is not null