我在Microsoft.Owin.Host.IIS (Helios) 上运行 Nancy。
我正在尝试连接 conneg viaIResponseProcessor
以响应 的Accept
标头text/plain
,但它只会返回 406。
我尝试了多种内容类型,但没有任何效果....除了,奇怪的是,text/html
(在清除基础之后ViewProcessor
)。
public class ViewApiProcessor : IResponseProcessor
{
private readonly IViewFactory viewFactory;
public ViewApiProcessor(IViewFactory _viewFactory)
{
this.viewFactory = _viewFactory;
}
private static readonly IEnumerable<Tuple<string, MediaRange>> extensionMappings =
new[] { new Tuple<string, MediaRange>("txt", MediaRange.FromString("text/plain")) };
public IEnumerable<Tuple<string, MediaRange>> ExtensionMappings
{
get { return extensionMappings; }
}
public ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context)
{
bool matchingContentType =
requestedMediaRange.Matches("text/plain");
return matchingContentType
? new ProcessorMatch { ModelResult = MatchResult.DontCare, RequestedContentTypeResult = MatchResult.ExactMatch }
: new ProcessorMatch();
}
public Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context)
{
context.ViewBag.RequestType = "api";
var response = (Response)this.viewFactory.RenderView(context.NegotiationContext.ViewName, model, GetViewLocationContext(context));
return response.WithContentType("text/plain");
}
private static ViewLocationContext GetViewLocationContext(NancyContext context)
{
return new ViewLocationContext
{
Context = context,
ModuleName = context.NegotiationContext.ModuleName,
ModulePath = context.NegotiationContext.ModulePath
};
}
}
然后,在模块中:
Get["/"] = p =>
{
return Negotiate.WithView("Index");
};
更新:我已经编辑了上面的代码以显示和的正确IResponseProcessor
组合Negotiator