如何共享使用“Response”的函数或“请求”在 ASP.NET 中?
我想要一个实用函数,可以有条件地更新我网站中多个页面的请求和响应。
使用标准 .CS 类似乎无法让我访问这些对象。我(一般来说)如何创建一个实用程序函数来检查 cookie 并在多个页面上更新它?
I'd like to have a utility function that conditionally updates my request and response across several pages in my site.
Using a standard .CS class doesn't seem to give me access to these objects. How can I (generall speaking) create a utility function that checks for a cookie and update it across multiple pages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
获取这些内容
您始终可以通过HttpContext 类 当前属性
为了管理整个站点中的一些 cookie 值,我建议创建一个所有页面都继承自的 BasePage 类,并在那里进行检查:
在 MasterPage 中执行相同的操作:
You can always get at these things via
HttpContext Class and the Current Property
And to manage some cookie value throughout your site I would suggest either create a BasePage class that all of your Pages inherited from and do the checks there:
do the same in your MasterPage:
使用 HttpContext.Current.Request 和 HttpContext.Current.Response
use HttpContext.Current.Request and HttpContext.Current.Response
使用完全限定的命名空间:
-- 或 --
那么您应该能够在整个类中访问请求/响应。
Use the fully qualified namespace:
-- or --
Then you should be able to access Request/Response throughout your class.
有几种方法可以做到这一点。其他人提到使用 System.Web.HttpContext.Current 执行此操作,但我认为(根据我认为您的意图猜测)在母版页上加载时运行的方法上执行此操作是一个更好的主意。
There are several ways to do this. Other have mentioned doing this with
System.Web.HttpContext.Current
, but I'd think (guessing from what I think your intent is) that doing this on a method that runs on load on your master pages is a better idea.