我创建了一个快捷方式GetCookie方法
public string GetCookieValue(string CookieName, string DefaultValue)
{
HttpCookie cookie = Request.Cookies[CookieName] ;
return cookie != null ? cookie.Value : DefaultValue;
}
但我想在default.aspx,usercontrol.ascx,webservice.asmx等中使用它... 我不想把它放在类库中,这样做我每次使用它时都需要传入Request对象。 我应该在哪里实施这种方法?我不知道Global.asax?
答案 0 :(得分:0)
扩展方法怎么样?
public static class RequestExtensions
{
public static string GetCookieValue(this HttpRequestBase request, string CookieName, string DefaultValue)
{
HttpCookie cookie = request.Cookies[CookieName] ;
return cookie != null ? cookie.Value : DefaultValue;
}
}
之后你可以打电话:
var cookie = Request.GetCookieValue(cookieName, defaultValue);
随时随地。
答案 1 :(得分:0)
我刚刚意识到网站上的任何类都可以使用 HttpContext.Current.Request / Response 。 所以我可以在站点中创建一个新的静态类来包含所有包含请求/响应的方法。 但问题是如何为要在页面中使用的Class.Method()创建别名?