11

System.TypeLoadException: Could not load type 'System.Web.HttpContext' from assembly 'System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Xml.Linq;

public class Example : 
{
XDocument doc = XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/example.xml"));
}
2
  • 2
    do you have a reference to system.web? Commented Feb 7, 2011 at 18:43
  • You should accept the answer so people know that you're now good... Commented Feb 7, 2011 at 18:54

2 Answers 2

5

Of course, if you are not in a web application, no way you can use the HttpContext.Current !

you can check if System.Web.HttpContext.Current is null or not, it's available only in web pages or web services.

Sign up to request clarification or add additional context in comments.

2 Comments

The follow up question then is how to return a relative path without Server.MapPath in C#.
since the MapPath is relative to the current virtual directory or web site, does not make sense to use it in a non web application. if you simply want to change a relative path into an absolute path in any location of the file system, Server.MapPath is NOT what you need.
5

Another issue is that the free community version seems to only work in .NET Framework and not .NET Core sadly.

1 Comment

'System.Web.HttpContext' from assembly 'System.Web' not solution for Net Core 3.1 ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.