【问题标题】:How to check "Session" from the .html page in `.NET`如何从`.NET`中的.html页面检查“会话”
【发布时间】:2017-12-15 06:24:30
【问题描述】:

基本上我试图从.html page 中检查Session 的值。所以我做了什么

在 ASPX 中 我将值存储在session 中并重定向到html 页面。

Session["user_id_no"] = "1234";
Response.Redirect(url);  

来自 HTML 我正在调用 REST GET 方法。

var check= $.ajax({
    url: "/loginCheck",
    type: 'GET',
    success: function (result) {
        return result;
    },
    async:false
});

REST API 和方法实现

[HttpGet]
[Route("loginCheck")]
public bool checkLoginStatus()
{
    SessionClass sc = new SessionClass();
    bool user_id_check = sc.check("user_id_no");       
    return user_id_check;
}

会话类实现为

public class SessionClass : System.Web.UI.Page
{
     public bool check(string sessionName)
     {
         if (Session[sessionName] == null)
             return false;
         return true;
     }
}

但是我遇到了异常。

HResult=-2147467259 Message=Session 状态只能在 enableSessionState 设置为 true 时使用,无论是在配置文件中还是在 Page 指令中。还请确保 System.Web.SessionStateModule 或自定义会话状态模块包含在应用程序配置的 \\ 部分中。

但是在我的web.config

<sessionState mode="InProc" timeout="30" />

我也遇到了一些问题

  1. How to access Session variables and set them in javascript?
  2. Getting session value in javascript
  3. 'Session' does not exist in the current context
  4. Session state can only be used when enableSessionState is set to true either in a configuration

【问题讨论】:

  • @Div from 4th no SO link 在接受的答案中说要配置任何一个 enableSessionState="true"sessionState mode="InProc" 我已经完成了第二个。

标签: javascript asp.net ajax rest session


【解决方案1】:

你试过这个吗?

using System.Web.SessionState;

public class SessionClass :  IRequiresSessionState
{
     public bool check(string sessionName)
     {
         if (HttpContext.Current.Session[sessionName] == null)
             return false;
         return true;
     }
}

【讨论】:

  • 我已经尝试过您的建议并收到此错误"Object reference not set to an instance of an object."
  • 你能调试吗,因为sn-p中没有代码行来引发这样的错误信息。
  • i.stack.imgur.com/NgqzQ.png 请检查一下我尝试过的内容。谢谢
【解决方案2】:

你需要在你的配置中enableSessionState 例外说

 <system.web>
      <pages enableSessionState="true" /> 
 </system.web>

查看this answer了解更多信息

【讨论】:

    猜你喜欢
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多