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 4613016
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:29:10+00:00 2026-05-22T01:29:10+00:00

I’m writing a project in c# asp.net mvc3, and I have a helper-class that

  • 0

I’m writing a project in c# asp.net mvc3, and I have a helper-class that looks like this:

using System.Collections.Generic;
using System.Web;

namespace CoPrice.Helpers
{
    public static class Messages
    {
        public static IList<Message> All
        {
            get
            {
                var list = ((IList<Message>)HttpContext.Current.Session["_messages"]) ?? new List<Message>();
                HttpContext.Current.Session["_messages"] = new List<Message>();
                return list;
            }
        }

        public static bool Exists
        {
            get
            {
                return (((IList<Message>)HttpContext.Current.Session["_messages"]) ?? new List<Message>()).Count > 0;
            }
        }

        public static void Add(MessageType type, string message)
        {
            Message m = new Message
            {
                Type = type,
                Text = message
            };
            HttpContext.Current.Session["_messages"] = HttpContext.Current.Session["_messages"] as List<Message> ?? new List<Message>();
            ((IList<Message>)HttpContext.Current.Session["_messages"]).Add(m);
        }

        public enum MessageType
        {
            Info,
            Success,
            Error
        }
        public struct Message
        {
            public MessageType Type;
            public string Text;
        }
    }
}

However, when I try to use these in a test, it crashes (cause of HttpContext.Current beeing null). How can I make this work both in tests and in the app itself? I don’t mind having to change this class to use something else than HttpContext.Current to access the session, but I want it to have properties as it does, so it can’t take the session-object as a parameter.

Any ideas on how to solve this problem?

  • 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-05-22T01:29:11+00:00Added an answer on May 22, 2026 at 1:29 am

    You need to define an IMessagesProvider and use an DI container to inject the implementation of the IMessagesProvider. In real use you’ll have an implementation that uses the ASP.Net session. In test use you’ll mostly mock it. BTW, you probably shouldn’t have a static Messages class. In fact, IMessagesProvider will probably replace your static Messages class.

    Ex:

    public class FooController : Controller
    {
        IMessagesProvider _messages;
    
        public FooController(IMessagesProvider messages)
        {
            // Your DI container will inject the messages provider automatically.
            _messages = messages;
        }
    
        public ActionResult Index()
        {
            ViewData["messages"] = _messages.GetMessages(Session.SessionId);
            return View();
        }
    }
    

    Mind you, this is a very simple example. In fact, you probably want one more class which is the model that drives the view. A quick intro:

    public ActionResult Index()
    {
        var model = PrepareModel(_messages);
        return View(model);
    }
    

    “PrepareModel” is your very own method that instantiates a new model class, fills it with the necessary data, and then you send it off to your view. It’s typical to have one model class per view defined. E.g. you’d have model classes like “SignupFormModel”, “DashboardModel”, “ChatModel”, etc., Doing so allows you to have strongly-typed views as well (a great thing).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

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.