C# Asp.Net WebApi自定义媒体格式化程序与特定类型不匹配

C# Asp.Net WebApi自定义媒体格式化程序与特定类型不匹配,c#,asp.net-web-api,C#,Asp.net Web Api,我正在检查WebAPI的内容协商和自定义格式化程序,并尝试使用通用自定义格式化程序。我正在使用RazorEngine解析一个razor视图,该视图将作为web api控制器的响应消息返回 这是我的格式化程序配置 GlobalConfiguration.Configuration.Formatters.Insert(0, new RazorViewFormatter<DefaultViewModel>("home-json", new MediaTypeHeaderVa

我正在检查WebAPI的内容协商和自定义格式化程序,并尝试使用通用自定义格式化程序。我正在使用RazorEngine解析一个razor视图,该视图将作为web api控制器的响应消息返回

这是我的格式化程序配置

        GlobalConfiguration.Configuration.Formatters.Insert(0, new RazorViewFormatter<DefaultViewModel>("home-json", new MediaTypeHeaderValue("text/json"), new MediaTypeHeaderValue("application/json")));
        GlobalConfiguration.Configuration.Formatters.Add(new RazorViewFormatter<DefaultViewModel>("home", new MediaTypeHeaderValue("text/html")));
        GlobalConfiguration.Configuration.Formatters.Add(new RazorViewFormatter<IEnumerable<CategoryViewModel>>("categories-json", new MediaTypeHeaderValue("text/json"), new MediaTypeHeaderValue("application/json")));
        GlobalConfiguration.Configuration.Formatters.Add(new RazorViewFormatter<IEnumerable<CategoryViewModel>>("categories", new MediaTypeHeaderValue("text/html")));
但是,向我的categories资源发出请求时,只返回我的模型的序列化json,而不是解析的模板。基本上缺少自定义媒体格式化程序上的标记。从未调用WriteToStreamAsync()

GET http://localhost:56071/api/categories HTTP/1.1
User-Agent: Fiddler
Accept: text/json
Host: localhost:56071
这不是一个可行的办法吗?我确实在CanReadType上找到了一个匹配项,我想知道多个格式化程序以及它们对conneg的求值方式是否最终与text/json不匹配

任何想法都很感激。谢谢

更新

public override bool CanWriteType(Type type)
{
    return (type == typeof (T) || type.IsSubclassOf(typeof (T)));
}

public override bool CanReadType(Type type)
{
    return false;
}

你能展示一下你的CanReadType吗?最后两个格式化程序都映射到IEnumerable,因此我不确定您将如何在这两者之间进行选择。另外,它应该是application/json而不是text/json
public override bool CanWriteType(Type type)
{
    return (type == typeof (T) || type.IsSubclassOf(typeof (T)));
}

public override bool CanReadType(Type type)
{
    return false;
}