Home > OS >  How to get a Context in a static method
How to get a Context in a static method

Time:08-21

What I am trying to achieve is to use a static class so that I can use it in all the pages/chtml. I have a table that will hold information like SiteName etc.

Usage of this would look like this:

@EnvTitles.GetPageInfo("sitename");

In the static class I need to produce this from a database table. Here is what I have:

public class EnvTitles
{
    private readonly GeneralEntities db = new GeneralEntities();

    public static string GetPageInfo(string info)
    {
        string ReturnVal = string.Empty;
        
        switch (info)
        {
            case "sitename":
                var site = db.SiteDescriptions.Where(x => x.Title == "Site Name").Single();
                ReturnVal = site.Text;
                break;

            default:
                ReturnVal = "";
                break;

        }

        return ReturnVal;
    }
}

The problem is it does not like db - "An object reference is required for the non-static field, method, or property 'EnvTitles.db'"

While searching I found an answer for this. Include the Context in the in the public static string. Like this:

public static string GetPageInfo(string info, GeneralEntities context)
{
        string ReturnVal = string.Empty;
        
        switch (info)
        {
            case "sitename":
                var site = context.SiteDescriptions.Where(x => x.Title == "Site Name").Single();
                ReturnVal = site.Text;
                break;

            default:
                ReturnVal = "";
                break;

        }

        return ReturnVal;
}

Which works, however now my ref to this on the page is expecting something in that place.

@EnvTitles.GetPageInfo("sitename", "Something Here!");

So there is nothing to put here. I tried just putting null there but when I ran the project it came back with context returned null.

Is there anyway to do this? Thanks for your help!

CodePudding user response:

The best practice is to pass the db context into your method like this

public static string GetPageInfo(string info, GeneralEntities db)

then create the GeneralEntities object in the calling cshtml so that it has the scope of the request. Your call will then look something like this in cshtml

@{
   var db = new GeneralEntities();
}

@EnvTitles.GetPageInfo("sitename", db);

There are many other ways to implement what you're trying to do but this is the most straight-forward, un-opinionated way.

CodePudding user response:

I got this to work by doing this:

  public static string GetPageInfo(string info)
    {
        string ReturnVal = string.Empty;
        var context = new GeneralEntities();
        switch (info)
        {
            case "sitename":
                var site = context.SiteDescriptions.Where(x => x.Title == "Site Name").Single();
                ReturnVal = site.Text;
                break;

            default:
                ReturnVal = "";
                break;

        }

        return ReturnVal;
    }

With the link on the page staying the same:

@EnvTitles.GetPageInfo("sitename")

Now I can add as many cases that I want and change the the link to that case. Thanks to all that helped!

  • Related