gpt4 book ai didi

c# - 是否有 WebMethod 的替代方案以绕过回发但访问 Session

转载 作者:太空宇宙 更新时间:2023-11-03 13:18:46 25 4
gpt4 key购买 nike

所以我正在尝试提高我们的 ASP.Net Webforms 应用程序的性能。

像往常一样,回传会尽力降低性能。我已经开始了解我也设法开始工作的 WebMethod 调用。然而,对于我的特定场景,我需要访问 Session,这在静态方法中显然是不可能的。

所以现在我的想法是编写一个 HttpModule 来解决这个问题。然而,这似乎需要很大的努力,所以我想知道你们中是否有人知道这项任务的开箱即用解决方案?

请注意,这只是一个关于某物是否已经存在的问题。编写模块会非常简单。我只是想避免重新发明轮子。

最佳答案

如果您的问题是从静态成员访问 Session 对象,您可以使用 HttpContext.Current.Session["..."]

[WebMethod(EnableSession = true)]
public static string Test()
{
string s = HttpContext.Current.Session["Test"].ToString();
return s;
}

引用here有关 HttpContext.Current Property 的文档;在该属性中,您还有 RequestResponse 等等。

正如@Martin Smellworse 所建议的

Wherever you set the session, you need EnableSessionState="ReadOnly" and Async="true" in the page declaration

关于c# - 是否有 WebMethod 的替代方案以绕过回发但访问 Session,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25083731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com