S Nash
S Nash

Reputation: 2499

ASP.NET server object not visible in a C# class

I have a ASP.NET WEB application and I added a Class to that app.

Lets call it a.cs

In this class in have:

using System.Web;

Still for the following line:

Server.MapPath("~\\bin\\error.txt")

I get "The name Server does not exist in the current context"

I researched further for the server object but I was directed to :

HttpServerUtility Class

Which appears to have all of the interface of the Server class.

Question: are these same HttpServerUtility and Server?

Why Server itself give me the mentioned error?

HttpServerUtility Class is accessible in the a.cs.

======================= The suggested duplicate solution is not a solution in my case: There the accepted answer is:

You need a reference to the System.Web assembly You need to get the class name right (HttpServerUtility, not HttpServerUtuility) You need to get the method name right (UrlDecode, not URLDecode) You need an instance of the class, as it's an instance method

None of above apply in my case.

Upvotes: 0

Views: 1851

Answers (3)

mason
mason

Reputation: 32704

Server is of type HttpServerUtility is a property of the Page class, and is set up by ASP.NET as part of the request lifecycle. To get an instance of HttpServerUtility in a standalone class you can use HttpContext.Current.Server. Like this:

var server = HttpContext.Current.Server;

Or in your class's functions, accept a context parameter.

public static void DoStuff(HttpContext context)
{
    var server = context.Server;
}

Upvotes: 4

David
David

Reputation: 218887

That's because Server doesn't exist in your class. It does exist in the Page class as a property. So when you do access Server in your page's code-behind, you're accessing a property, not the class itself.

That property can also be accessed here:

System.Web.HttpContext.Current.Server

Current is a static property on the HttpContext class which refers to the current HTTP context, which is initialized by ASP.NET in a running web application. Note that if you do this, your class will be tightly coupled with a running HTTP context. This generally isn't a good practice.

Instead, what you might do is require an instance of a valid HttpContext in your class. Something like this:

public class MyClass
{
    private HttpContext _currentContext;

    public MyClass(HttpContext currentContext)
    {
        _currentContext = currentContext;
    }

    // the rest of your class
}

Then any time something needs to create an instance of your class, it also needs to supply the HttpContext which your class would use. This allows your class to be used an tested independently of any given HTTP context, as long as whatever uses it can supply one.

You can further de-couple this if your class doesn't need the whole context but instead just needs some component(s) or value(s) from it. Generally speaking, the less coupled your objects are the more portable and re-usable they are.

Upvotes: 1

Sergey Litvinov
Sergey Litvinov

Reputation: 7458

Because Server is a property of the page. But looks like you aren't inherit page class in your a.cs so it isn't available. You can also use HttpContext.Current.Server

Server property is instance of HttpServerUtility, and there are a couple of static methods and a lot of instance methods, so depending on what you want you can also use static way.

Upvotes: 1

Related Questions