问题
In Visual Studio 2010, I am using ASP.NET MVC 4 for creating a web api project as in this sample : https://www.youtube.com/watch?v=H9vBxAH4f5E
Things work fine - I succeed using GET
method, but when using POST
method, I encounter a problem:
No MediaTypeFormatter is available to read an object of type 'String' from content with media type 'application/x-www-form-urlencoded'.
My webapiconfig.cs
looks like this:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
FormUrlEncodedMediaTypeFormatter f;
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
f = new FormUrlEncodedMediaTypeFormatter();
f.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.Formatters.Add(f);
// application/x-www-form-urlencoded ???
}
}
Why am I encounter a problem:
No MediaTypeFormatter is available to read an object of type 'String' from content with media type 'application/x-www-form-urlencoded'.
The full error message:
Message: "An error has occurred." ExceptionMessage: "No MediaTypeFormatter is available to read an object of type 'String' from content with media type 'application/x-www-form-urlencoded'." ExceptionType: "System.InvalidOperationException" StackTrace: " at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable
1 formatters, IFormatterLogger formatterLogger) at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable
1 formatters, IFormatterLogger formatterLogger) at System.Web.Http.ModelBinding.FormatterParameterBinding.ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken) at System.Web.Http.Controllers.HttpActionBinding.<>c__DisplayClass1.b__0(HttpParameterBinding parameterBinder) at System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext() at System.Threading.Tasks.TaskHelpers.IterateImpl(IEnumerator
1 enumerator, CancellationToken cancellationToken)"
The request header:
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.103 Safari/537.36 Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo Content-Type: application/x-www-form-urlencoded Accept: */* Accept-Encoding: gzip,deflate Accept-Language: he-IL,he;q=0.8,en-US;q=0.6,en;q=0.4
The response header:
Cache-Control: no-cache Pragma: no-cache Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Mon, 08 Sep 2014 18:06:10 GMT Content-Length: 1111
How can I solve the problem?
Thanks :)
回答1:
You should just need to add the new media type to the list of supported formats:
f = new FormUrlEncodedMediaTypeFormatter();
f.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
f.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/x-www-form-urlencoded"));
来源:https://stackoverflow.com/questions/25729184/asp-net-mvc-4-using-post-method