开发者

Elegant way building url request with parameters

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 co开发者_开发技巧ncatenation of strings is a litte bit old school, no?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜