问题
My project framework : .net 4.5
Project Type : MVC4
I have added System.Json of framework 4.0.
I have following code into Model -> JsonNetFormatter.cs and it's inheriting MediaTypeFormatter class:
protected override System.Threading.Tasks.Task<object> OnReadFromStreamAsync(Type type, System.IO.Stream stream, System.Net.Http.Headers.HttpContentHeaders contentHeaders, FormatterContext formatterContext)
{
var task = Task<object>.Factory.StartNew(() =>
{
var settings = new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore,
};
var sr = new StreamReader(stream);
var jreader = new JsonTextReader(sr);
var ser = new JsonSerializer();
ser.Converters.Add(new IsoDateTimeConverter());
object val = ser.Deserialize(jreader, type);
return val;
});
return task;
}
but when i am going to build solution or my project, it's throwing following error :
The type or namespace name 'FormatterContext' could not be found (are you missing a using directive or an assembly reference?)
Am i missing any dll or because of something else, it's throwing an error?
回答1:
FormatterContext
is removed in MVC4
RC Version
Please refer the Release Notes: Release notes
from : MSDN Forums
Formatter improvements: The methods on MediaTypeFormatter are now public to enable unit testing of custom formatters. A single formatter can now support multiple text encodings. Use BufferedMediaTypeFormatter to implement simple synchronous formatting support. FormatterContext has been removed. To get access to the request from a formatter on the server implement GetPerRequestFormatterInstance.
来源:https://stackoverflow.com/questions/20095701/the-type-or-namespace-name-formattercontext-could-not-be-found-net-4-5