开发者

make HttpCookie shorter [closed]

开发者_JAVA技巧 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

Can we make the code below shorter. Thanks

HttpCookie myCookie = new HttpCookie("UserID", "JLovus");
myCookie.Expires = DateTime.Now.AddMinutes(30);
Response.Cookies.Add(myCookie);


This really isn't much code. I can imagine it getting annoying if you have to do it more than once, though. In that case, you could use a function to do it for you. For example:

public static class ResponseExtensions 
{
    public static void AddCookie(this HttpResponse response, string key, string value, 
        int minutesToLive = 30) 
    {
        var myCookie = new HttpCookie(key, value);
        myCookie.Expires = DateTime.Now.AddMinutes(minutesToLive);
        response.Cookies.Add(myCookie);
    }
}

Your code then becomes:

Response.AddCookie("UserID", "JLovus");


Response.Cookies.Add(new HttpCookie("UserID", "JLovus")
{
    Expires = DateTime.Now.AddMinutes(30)
}); 


You could do it in 1 less-readable line as follows: Response.Cookies.Add(new HttpCookie("UserID", "JLovus") {Expires = DateTime.Now.AddMinutes(30)});

Just because you can do something in less lines of code doesn't mean you should do it that way. Your original code was concise and easily readable. What I (and several other people) posted is harder to understand at a glance.


Response.Cookies.Add(new HttpCookie("UserID", "JLovus")
{
    Expires = DateTime.Now.AddMinutes(30)
});


If you're using this code a lot, you should consider putting it into a function:

protected void CreateUserIDCookie(string userID, int timeUntilExpires)
{
    HttpCookie myCookie = new HttpCookie("UserID", userID);
    myCookie.Expires = DateTime.Now.AddMinutes(timeUntilExpires);
    Response.Cookies.Add(myCookie);
}

Then in your code just do this:

this.CreateUserIDCookie("JLovus", 30);

As others have suggested, you can reduce it to a single line, but as they've also said, it's less readable, and I'd rather have three lines of readable code, than a single line that is harder to read. Code is already hard enough to read.


Yes, but what is the point?

Response.Cookies.Add(new HttpCookie("UserID", "JLovus") 
    {Expires = DateTime.Now.AddMinutes(30)});


You can place it inside a function:

public HttpCookie AddHttpCookie(String key, String value, int timeOut)
{
    HttpCookie httpCookie = new HttpCookie(key, value);
    httpCookie.Expires = DateTime.Now.AddMinutes(timeOut);
    Response.Cookies.Add(httpCookie);
    return httpCookie;
}

And call it this way:

HttpCookie myCookie = AddHttpCookie("UserID", "JLovus", 30);

Or just:

AddHttpCookie("UserID", "JLovus", 30);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜