在asp页面中创建方法前饼干 [英] create method fore cookies in asp pages
问题描述
//这个代码在新类中称为实用程序,而不是在表单加载中,当我需要在实用程序类中编写请求或响应时,它从未写过响应或请求,我只看到httpresponse我需要看到响应,我希望你详细解释一下这究竟是什么,所以没有错误,并在实用程序类中执行>>>>>>>>请看下面的评论。
公共类实用程序
{
public void createCookie(string cookieName,string [] keys,string [] values,bool expired)
{
HttpCookie c = new HttpCookie(cookieName);
if(keys!= null)
{
for(int x = 0; x& lt; keys.Length; x ++)
c.Values.Add(keys [x],values [x]);
if(!expired)
c.Expires = DateTime.Now.AddYears(5);
}
else
c.Expires = DateTime.Now.AddYears(-5);
//查看此步骤>>>>>>>
res.Cookies。添加(c);
}
public string readFromCookies( string cookieName,string key,HttpRequest req)
{
try
{
返回req.Cookies [cookieName] [key] .ToString();
}
catch
{
返回null;
}
}
public void removeCookie( string cookieName,HttpResponse res)
{
//并查看此步骤>>>>>>>>>> >>>>>
createCookie(cookieName,null,null,false,res);
}
}< / pre>
//this code in new class called utility , not in form load , when i need to write request or response in utility class it never written just "response or request " , i see only httpresponse i need to see just response , I hope that you explain to me in detail what exactly do so there are not mistakes, and execute in utilitys class >>>>>>>> look in Comment below.
public class utilitys
{
public void createCookie(string cookieName, string[] keys, string[] values, bool expired)
{
HttpCookie c = new HttpCookie(cookieName);
if (keys != null)
{
for (int x = 0; x < keys.Length; x++)
c.Values.Add(keys[x], values[x]);
if (!expired)
c.Expires = DateTime.Now.AddYears(5);
}
else
c.Expires = DateTime.Now.AddYears(-5);
// look in this step >>>>>>>
res.Cookies.Add(c);
}
public string readFromCookies(string cookieName, string key, HttpRequest req)
{
try
{
return req.Cookies[cookieName][key].ToString();
}
catch
{
return null;
}
}
public void removeCookie(string cookieName, HttpResponse res)
{
// and look in this step >>>>>>>>>>>>>>>
createCookie(cookieName, null, null, false, res);
}
}</pre>
推荐答案
你可以用createCookie方法中的HttpContext.Current.Response替换res
you can replace res with HttpContext.Current.Response in createCookie method
HttpContext.Current.Response.Cookies.Add(c);
或
你可以在方法中添加新的HttpResponse参数并将其作为参数传递给方法
OR
you can add new HttpResponse parameter to the method and pass it to the method as parameter
public void createCookie(string cookieName, string[] keys, string[] values, bool expired, HttpResponse res)
{
HttpCookie c = new HttpCookie(cookieName);
if (keys != null)
{
for (int x = 0; x < keys.Length; x++)
c.Values.Add(keys[x], values[x]);
if (!expired)
c.Expires = DateTime.Now.AddYears(5);
}
else
c.Expires = DateTime.Now.AddYears(-5);
// look in this step >>>>>>>
res.Cookies.Add(c);
}
这篇关于在asp页面中创建方法前饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!