0

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.

S Nash
  • 2,363
  • 3
  • 34
  • 64
  • possible duplicate of [Can't find HttpServerUtility class in System.Web in C#](http://stackoverflow.com/questions/1368596/cant-find-httpserverutility-class-in-system-web-in-c-sharp) – Chris Farmer May 04 '15 at 17:06

3 Answers3

3

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;
}
mason
  • 31,774
  • 10
  • 77
  • 121
  • Looking at Microsoft help : `https://msdn.microsoft.com/en-us/library/system.web.httpserverutility%28v=vs.110%29.aspx` And I do not see Server as a Property of HttpServerUtility – S Nash May 04 '15 at 17:39
  • @Nash I didn't say it was. Re-read my answer. I said the type of the property was HttpServerUtility. – mason May 04 '15 at 17:49
  • So I can pass the var server as the context parameter to DoStuff function? – S Nash May 04 '15 at 18:11
  • @SNash No. You either do one approach or the other. Get the context (and thus the server) from the page via function or constructor parameter or use `HttpContext.Current.Server`. – mason May 04 '15 at 18:15
1

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.

Sergey Litvinov
  • 7,408
  • 5
  • 46
  • 67
1

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.

David
  • 208,112
  • 36
  • 198
  • 279
  • The only reason I need to access Server in this class is to use 'Server.MapPath("~\\bin\\error.txt")' . So i need whaever you mentioned above? – S Nash May 04 '15 at 17:44
  • @SNash: In that case I would recommend requiring either `HttpContext` or `HttpServerUtility` on the class' constructor. `HttpServerUtility` requires an active HTTP context in order for that method to work, so at the very least this class is naturally coupled to a web application. Which one gets passed to the constructor makes little difference. I'd lean toward narrowing the scope as much as possible, which would require an instance of `HttpServerUtility` on the constructor. – David May 04 '15 at 17:50