开发者

Elegant way building url request with parameters

开发者 https://www.devze.com 2022-12-11 17:51 出处:网络
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

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

精彩评论

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

关注公众号