ASP.NET MVC 4-使用发布方法 [英] ASP.NET MVC 4 - using post method
问题描述
在Visual Studio 2010中,我使用ASP.NET MVC 4来创建Web api项目,如以下示例所示: https://www.youtube.com/watch?v=H9vBxAH4f5E
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
一切正常-我成功使用GET
方法,但是使用POST
方法时,我遇到了问题:
Things work fine - I succeed using GET
method, but when using POST
method, I encounter a problem:
没有MediaTypeFormatter可用来读取类型为'String'的对象 媒体类型为'application/x-www-form-urlencoded'的内容.
No MediaTypeFormatter is available to read an object of type 'String' from content with media type 'application/x-www-form-urlencoded'.
我的webapiconfig.cs
看起来像这样:
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 ???
}
}
我为什么遇到问题:
没有MediaTypeFormatter可用来读取类型为'String'的对象 媒体类型为'application/x-www-form-urlencoded'的内容.
No MediaTypeFormatter is available to read an object of type 'String' from content with media type 'application/x-www-form-urlencoded'.
完整的错误消息:
消息:发生错误." ExceptionMessage:否 MediaTypeFormatter可用于读取字符串"类型的对象 从媒体类型为"application/x-www-form-urlencoded"的内容中删除." ExceptionType:"System.InvalidOperationException" StackTrace:位于" System.Net.Http.HttpContentExtensions.ReadAsAsync [T](HttpContent 内容,类型类型,IEnumerable
1 formatters, IFormatterLogger formatterLogger) at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable
1格式化程序,IFormatterLogger formatterLogger)在 System.Web.Http.ModelBinding.FormatterParameterBinding.ExecuteBindingAsync(ModelMetadataProvider metadataProvider,HttpActionContext actionContext,CancellationToken cancelToken) System.Web.Http.Controllers.HttpActionBinding.<> c__DisplayClass1.b__0(HttpParameterBinding parameterBinder)在 System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext() at System.Threading.Tasks.TaskHelpers.IterateImpl(IEnumerator
1 枚举,CancellationToken cancelToken)"
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)"
请求标头:
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
响应头:
缓存控制:无缓存语法:无缓存内容类型: application/json; charset = utf-8过期:-1服务器: Microsoft-IIS/8.0 X-AspNet版本:4.0.30319 X-Powered-通过:ASP.NET 日期:2014年9月8日星期一18:06:10 GMT内容长度:1111
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
我该如何解决问题?
谢谢:)
推荐答案
您只需要将新的媒体类型添加到支持的格式列表中即可:
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"));
这篇关于ASP.NET MVC 4-使用发布方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!