【问题标题】:Web API returning JSON content type instead of XMLWeb API 返回 JSON 内容类型而不是 XML
【发布时间】:2013-05-08 23:04:48
【问题描述】:

我是 Web API 的新手。我正在为我的团队开发一个概念验证项目,我们将根据 Web-API XML 数据源创建 SSRS 2012 报告。但是,不应将 Web-API 配置为仅协商 XML 作为内容类型。在未来阶段,我们的 Web 应用程序应该能够从相同的控制器/操作中检索 json 对象。

我从关注this tutorial 开始,一切正常,没问题。

接下来我配置了我的路由,以便我可以直接调用操作,并将 QueryStringMappings 添加到我的 Global.asax 中,以便我可以指定内容类型。

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { id = UrlParameter.Optional }
        );
    }
}

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        GlobalConfiguration.Configuration.Formatters.JsonFormatter.AddQueryStringMapping("$format", "json", "application/json");
        GlobalConfiguration.Configuration.Formatters.XmlFormatter.AddQueryStringMapping("$format", "xml", "application/xml");
    }
}

这很好用,我现在可以使用 localhost:XXXXX/api/Products/GetAllProducts/?$format=xml 作为 SSRS 中的数据源连接字符串。我不确定这是否是最好的方法,但它确实有效,所以我坚持了下来。如果没有查询字符串映射,则数据源无法在 SSRS 中工作。

这是我遇到麻烦的地方。我分支并创建了我的第一个模型/控制器/动作。当我在浏览器(chrome 或 IE 10)中运行项目并尝试将格式指定为 XML(localhost:XXXXX/api/Calcs/ComputeFooCalculation?$format=xml)时,我得到了一个 json 结果。 Products 控制器继续适用于 json 或 xml 内容类型。但由于某种原因,我的操作只会呈现为 json。这是我的代码的样子。如果您需要模型或其他任何东西,请告诉我。 FooCalculation 有一个嵌套对象 Bar。 FooCalculation 和 Bar 都有字符串、双精度和日期时间。

控制器:

public class CalcsController : ApiController
{

    public FooCalculation ComputeFooCalculation()
    {
        var Foo = GetFoo();
        var Bar = GetBar();
        var FooCalculation = new FooCalculation(Foo, Bar);

        return FooCalculation;
    }

}

示例 JSON 结果:

{"Foo":"XXX","FooRate":{"Foo":"XXX","Bar":"SN","FooBar":-1.00813E-05,"BarFoo":-3.2644199999999995E-06,"FoooBarrr":-4.17501E-06,"BarDate":"2013-05-14T00:00:00"},"BarRate":{"Foo":"XXX","Bar":"1W","FooBar":-2.08687E-05,"BarFoo":-3.11313E-05,"FoooBarrr":-3.3E-05,"BarDate":"2013-05-21T00:00:00"},"BarDate":"2013-05-20T00:00:00","FooDays":6,"FooBar":-7.3741306716417904E-06,"Bar":-0.0011149741306716415}

提前感谢您的帮助。

【问题讨论】:

    标签: xml json api web reporting-services


    【解决方案1】:

    在模型类中放置一个无参数构造函数。 XML 格式试图在您检索所有数据之前实例化一个空的 XML 树。

    【讨论】:

    • 谢谢,这很有效!解决此问题的另一种方法是使用 DataContract 属性 (System.Runtime.Serialization)。这允许您指定 XML 树。
    • 我不知道这是如何被接受的答案,因为它对我不起作用。正确的做法是将此行添加到 WebApiRegister.Register:config.Formatters.Remove(config.Formatters.XmlFormatter);
    • 当存在一些 XML 验证问题时,通常会出现这种情况。像一些缺少的无参数构造函数一样,一些 xml 属性定义了两次或不可序列化的子类型等。首先尝试先通过一些单元测试检查您的类/层次结构是否完全可序列化。那就去别的地方看看吧。
    猜你喜欢
    • 2013-08-18
    • 1970-01-01
    • 2017-12-15
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多