Windows 2003 Server上没有MediaTypeFormatter

时间:2012-05-01 22:14:55

标签: windows-server-2003 asp.net-web-api

我有一个非常直接的ApiController在Win7上工作正常但在Windows 2003 Server上我收到错误。

获取请求(来自浏览器或$ .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>

我正在使用5/1/2012 nuget nightly build软件包。看起来objectContent.Value在Windows 2003 Server上是空的,但在HttpContentExtensions.cs的以下行中没有在Windows 7上:

        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 vs Windows 2003 Server行为。如果我删除控制器操作上的InputModel参数,则在Win7和Windows 2003 Server上运行时将调用该操作。但是在Win7上,它从GET和POST请求中返回JSON。在Windows 2003 Server上,它从POST返回GET和JSON的XML。

这导致我使用POST测试InputModel参数。我已经验证正确调用了该操作,并且在Windows 2003 Server上绑定了InputModel参数,但仅限于使用POST时。因此,解决方法是在GET上手动获取参数。这允许jQuery的$ .getJSON在Windows 2003 Server下对自托管服务器起作用:

[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();
    }
}

1 个答案:

答案 0 :(得分:0)

您可以显示配置的格式化程序以及InputModel类吗?看起来序列化程序无法处理您的类。您是否在任何属性上使用接口?