我有一個非常直接的ApiController,它可以在Win7上正常工作,但在Windows 2003 Server上出現錯誤。在Windows 2003服務器上沒有MediaTypeFormatter可用
GET請求(從瀏覽器或$ .getJson):
https://site.com:61656/AD/Authenticate?UserName=xxxx&Password=xxxxx&AuthKey=xxxxxx
我得到以下錯誤:
<Exception xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/System.Web.Http.Dispatcher">
<ExceptionType>System.InvalidOperationException</ExceptionType>
<Message>
No MediaTypeFormatter is available to read an object of type 'InputModel' from content with media type ''undefined''.
</Message>
<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.<ExecuteBindingAsync>b__0(HttpParameterBinding parameterBinder) at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext() at System.Threading.Tasks.TaskHelpers.IterateImpl(IEnumerator`1 enumerator, CancellationToken cancellationToken)
</StackTrace>
</Exception>
我使用的是2012年5月1日的NuGet每晚構建包。它看起來像objectContent.Value通過空未來Windows 2003 Server上而不是在Windows 7中HttpContentExtensions.cs以下行:
if (objectContent != null && objectContent.Value != null && type.IsAssignableFrom(objectContent.Value.GetType()))
{
return TaskHelpers.FromResult((T)objectContent.Value);
}
控制器動作:
[AcceptVerbs("GET", "POST")]
public ResultModel Authenticate(InputModel inputModel)
{
var test = ControllerContext.Request.Content.Headers.ContentType;
//Console.WriteLine(test.MediaType);
try
{
Console.WriteLine("AD Authorize request received: " + inputModel.UserName);
var ldap = new LdapAuthentication();
return ldap.Authenticate(inputModel);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return new ResultModel();
}
}
的MediaType來通過Win7上的null,但在Windows 2003服務器上,請求永遠不會使其進入控制器操作。
有沒有辦法指定一個默認媒體格式化程序來處理「'undefined'」媒體類型?
編輯:
這裏是輸入模型:
public class InputModel {
public string UserName { get; set; }
public string Password { get; set; }
public string AuthKey { get; set; }
}
這裏是(自主機)配置:
var config = new HttpsSelfHostConfiguration(ConfigurationManager.AppSettings["serviceUrl"]);
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
config.Routes.MapHttpRoute("default", "{controller}/{action}");
var server = new HttpSelfHostServer(config);
try
{
server.OpenAsync().Wait();
Console.WriteLine("Waiting...");
編輯#2:
我有注意到更多有趣的Win7與Windows 2003 Server行爲。如果我在控制器動作上刪除了InputModel參數,則在Win7和Windows 2003 Server上運行時都會調用該動作。但是,在Win7上,它會從GET請求中返回JSON。在Windows 2003 Server上,它從POST返回GET和JSON中的XML。
這導致我使用POST測試InputModel參數。我已驗證該操作是否正確調用,並且InputModel參數綁定在Windows 2003 Server上,但僅限於使用POST時。所以解決方法是在GET上手動獲取參數。這使得jQuery的$ .getJSON反對自託管服務器工作在Windows 2003服務器:
[AcceptVerbs("GET")]
public ResultModel Authenticate()
{
try
{
var inputModel = new InputModel();
var query = ControllerContext.Request.RequestUri.ParseQueryString();
inputModel.UserName = query.GetValues("UserName") != null ? query.GetValues("UserName")[0] : null;
inputModel.Password = query.GetValues("Password") != null ? query.GetValues("Password")[0] : null;
inputModel.AuthKey = query.GetValues("AuthKey") != null ? query.GetValues("AuthKey")[0] : null;
Console.WriteLine("AD Authorize request received: " + inputModel.UserName);
var ldap = new LdapAuthentication();
return ldap.Authenticate(inputModel);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return new ResultModel();
}
}
對此有何好運?我有同樣的問題。 –