我相信如果您的 ASP.NET 路由设置正确,您不需要指定 ActionName,例如:
protected void Application_Start()
{
RouteTable.Routes.MapHttpRoute("0", "{controller}/{action}/{arg1}");
}
将匹配/YourControllerName/Retrieve/132
您返回的内容完全基于您的媒体类型格式化程序,其中默认为XmlFormatter 和JsonFormatter。这些可以在GlobalConfiguration.Configuration.Formatters 中找到,并将根据客户端提供的Accept 标头进行选择。
例如,我们将JSON.Net 用于我们的响应格式,由以下人员配置:
protected void Application_Start()
{
RouteTable.Routes.MapHttpRoute("0", "{controller}/{action}/{arg1}");
MediaTypeFormatterCollection formatters = GlobalConfiguration.Configuration.Formatters;
formatters.Remove(formatters.XmlFormatter);
var jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
jsonFormatter.Formatting = Formatting.Indented;
jsonFormatter.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
这告诉 WebApi 禁止任何 XML 格式,并且仅使用提供的 JSON.Net 合同解析器返回 JSON。 JSON.Net 支持序列化IEnumerable。
不过,我建议改为返回 HttpResponseMessage。这也允许您设置状态代码(这仍然使用媒体类型格式化程序,它只是一个更干净的包装器)。你可以这样使用:
[HttpGet]
public HttpResponseMessage Retrieve(int subjectId)
{
var response _referenceService.Retrieve(subjectId);
return Request.CreateResponse(HttpStatusCode.OK, response);
}