2012-10-13 33 views
0

我在ASP.NET中做了第一步,我遇到了類的問題。我想創建一個新的自定義類來支持會話並收集有關我網站上用戶數量的信息。 我已經在文件MySession.cs中創建了一個類,該類已被WebMatrix放在名爲「App_data」的目錄中。 當我嘗試在.cshtml文件中使用這個類時,它會引發我無法找到該類的信息。 我在網上找到了,該類應該放在App_Code中,所以我已經完成了。但是,在這一刻,它向我顯示了錯誤,類似「請求」的類無法找到。如何在WebMatrix v2中使用自定義類?

如何在WebMatrix中使用自定義類?

我的C#從.cshtml文件的代碼如下所示:

@{ 
    MySession session = new MySession(60); 
    session.start(); 
    var db = Database.Open("studia"); 
    var data = db.Query("SELECT COUNT(*) as total FROM sessions"); 
} 

和類文件的樣子:

using System; 
using System.Collections.Generic; 
using System.Web; 
using System.Security.Cryptography; 
using System.Data; 
using System.Data.SqlClient; 

    /// <summary> 
    /// Summary description for sesss 
    /// </summary> 
    public class MySession 
    { 
     private String _session_id; 
     private int _session_time; 

     public MySession(int session_time = 60) 
     { 
      _session_time = session_time; 
      using (MD5 md5Hash = MD5.Create()) 
      { 
       _session_id = (Request.Cookies["session_id"] != null) ? Server.HtmlEncode(Request.Cookies["sesion_id"].Value) : GetMd5Hash(md5Hash, DateTime.Now); 
      } 
      cleanup(); 
     } 

     public bool isLogged() 
     { 
      if (Request.Cookies["session_id"] != null) 
      { 
       return true; 
      } 
      return false; 
     } 

     public void start(string username) 
     { 
      DateTime now = DateTime.Now; 

      var db = Database.Open("studia"); 

      if (isLogged()) 
      { 
       db.Query("UPDATE sessions SET start_time = " + now + " WHERE session_id = " + _session_id); 
      } 
      else 
      { 
       db.Query("INSERT INTO sessions (id, start_time, username) VALUES ('" + _session_id + "', '" + now + "', '" + username + "'"); 
      } 

      HttpCookie session_cookie = new HttpCookie("session_id"); 
      session_cookie.Value = DateTime.Now; 
      session_cookie.Expires = DateTime.Now.AddSeconds(_session_time); 
      Response.Cookies.Add(aCookie); 
     } 

     public void cleanup() 
     { 
      var db = Database.Open("studia"); 
      db.Query("DELETE FROM sessions WHERE start_time < " + (DateTime.Now - _session_time)); 
     } 

     static string GetMd5Hash(MD5 md5Hash, string input) 
     { 

      // Convert the input string to a byte array and compute the hash. 
      byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); 

      // Create a new Stringbuilder to collect the bytes 
      // and create a string. 
      StringBuilder sBuilder = new StringBuilder(); 

      // Loop through each byte of the hashed data 
      // and format each one as a hexadecimal string. 
      for (int i = 0; i < data.Length; i++) 
      { 
       sBuilder.Append(data[i].ToString("x2")); 
      } 

      // Return the hexadecimal string. 
      return sBuilder.ToString(); 
     } 

     // Verify a hash against a string. 
     static bool VerifyMd5Hash(MD5 md5Hash, string input, string hash) 
     { 
      // Hash the input. 
      string hashOfInput = GetMd5Hash(md5Hash, input); 

      // Create a StringComparer an compare the hashes. 
      StringComparer comparer = StringComparer.OrdinalIgnoreCase; 

      if (0 == comparer.Compare(hashOfInput, hash)) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 

    } 

回答

1

如果你想引用Request對象的一類(而不是從一個頁面文件中),你需要使用HttpContext.Current例如

public bool isLogged() 
{ 
    return HttpContext.Current.Request.Cookies["session_id"] != null; 
} 
+0

好的,但是類中的數據庫對象呢? – deem

+1

您可能需要使用using語句來引用WebMatrix.Data。哦,並且服務器也需要HttpContext.Current。 –

相關問題