Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8688041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:15:45+00:00 2026-06-12T23:15:45+00:00

I do first steps in the ASP.NET and I’ve a issue with classes. I

  • 0

I do first steps in the ASP.NET and I’ve a issue with classes. I would like to create a new custom class to support sessions and collect information about the number of users on my website.
I’ve created a class in file MySession.cs, which has been put in the dir called “App_data” by WebMatrix.
When I try to use this class in .cshtml file, it throws me information it couldn’t found that class.
I found in the Web, the class should be placed in App_Code, so I’ve done it. However, in this moment it shows me an error that classes like “Request” couldn’t been found.

How to use custom class in WebMatrix?

My c# code from .cshtml file looks like:

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

and class file looks like:

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:15:46+00:00Added an answer on June 12, 2026 at 11:15 pm

    If you want to refer to the Request object in a class (as opposed to from within a page file) you need to use HttpContext.Current e.g.

    public bool isLogged()
    {
      return HttpContext.Current.Request.Cookies["session_id"] != null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just tried my first steps with grid . I would like to set
I'm taking my first steps in c # & asp.net and I'm enjoying it
Hai guys, I started a new asp.net mvc application and i followed the steps
I have a ASP.Net create user wizard. One of the additional steps I included
I want learn, how to create a custom module in orchard cms (ASP.net mvc3).
What are first steps creating a loadable DLL module extension for PHP to create
I am working on asp.net mvc. I am trying to implement Code-First Entity Model.
I'm trying to create a login and registration form in ASP.net and I came
I am trying my hands on Visual Studio 2010 (Asp.Net 4.0) for the first
I am trying to create an ASP.NET page that connects to QuickBooks Online Edition,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.