UriPathExtensionMapping

UriPathExtensionMapping

本文介绍了UriPathExtensionMapping来控制的WebAPI响应格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有越来越UriPathExtensionMapping一个问题,在ASP.NET的WebAPI工作。我的设置如下:

我的路线是:

config.Routes.MapHttpRoute(
                名称:阿比UriPathExtension
                routeTemplate:API / {控制器} {扩展} / {显示}。
                默认:新{ID = RouteParameter.Optional}
            );            config.Routes.MapHttpRoute(
               名称:阿比UriPathExtension ID,
                routeTemplate:API / {控制器} / {ID} {}扩展。
                默认:新{ID = RouteParameter.Optional}
            );            config.Routes.MapHttpRoute(
                名称:DefaultApi
                routeTemplate:API / {}控制器/(编号),
                默认:新{ID = RouteParameter.Optional}
            );

我的全球ASAX文件是:

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

我的控制器是:

公开的IEnumerable<串GT;得到()
{
    返回新的字符串[] {盒,矩形};
}// GET / API /价值/ 5
公共字符串GET(INT ID)
{
    返回盒子;
}// POST / API /值
公共无效后(字符串值)
{
}// PUT / API /价值/ 5
公共无效认沽(INT ID,字符串值)
{
}//删除/ API /价值/ 5
公共无效删除(INT ID)
{
}

在制作使用curl请求,JSON是默认响应,甚至当我明确要求XML我仍然得到JSON:

 卷曲的http://localhost/eco/api/products/5.xml

返回:

 http://www.google.com

任何人都可以看到的问题与我的设置?

以下code映射在Global.asax文件扩展名的路线已配置完成后:

GlobalConfiguration.Configuration.Formatters.JsonFormatter。
        MediaTypeMappings.Add(
            新UriPathExtensionMapping(
                JSON,应用/ JSON
        )
    );    GlobalConfiguration.Configuration.Formatters.XmlFormatter。
        MediaTypeMappings.Add(
            新UriPathExtensionMapping(
                XML,应用程序/ XML
        )
    );


解决方案

您需要注册,像这样的扩展名映射:

config.Formatters.JsonFormatter.MediaTypeMappings.Add(新UriPathExtensionMapping(JSON,应用/ JSON));
config.Formatters.XmlFormatter.MediaTypeMappings.Add(新UriPathExtensionMapping(XML,应用程序/ XML));

例被发现。

更新

如果你看一下code的<$c$c>UriPathExtensionMapping为扩展名的占位符。

///&LT;总结&gt;
///在&lt;见CREF =T:的System.Uri/&GT;路径扩展的关键。
///&LT; /总结&gt;
公共静态只读字符串UriPathExtensionKey =转;

所以,你的路线将需要改变({}分机不{}扩展):

config.Routes.MapHttpRoute(
            名称:阿比UriPathExtension
            routeTemplate:API / {控制器} {转} / {显示}。
            默认:新{ID = RouteParameter.Optional}
        );

I'm having a problem getting UriPathExtensionMapping working in ASP.NET WebAPI. My setup is as follows:

My routes are:

            config.Routes.MapHttpRoute(
                name: "Api UriPathExtension",
                routeTemplate: "api/{controller}.{extension}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            config.Routes.MapHttpRoute(
               name: "Api UriPathExtension ID",
                routeTemplate: "api/{controller}/{id}.{extension}",
                defaults: new { id = RouteParameter.Optional }
            );

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

My Global ASAX file is:

    AreaRegistration.RegisterAllAreas();

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

My Controller is:

public IEnumerable<string> Get()
{
    return new string[] { "Box", "Rectangle" };
}

// GET /api/values/5
public string Get(int id)
{
    return "Box";
}

// POST /api/values
public void Post(string value)
{
}

// PUT /api/values/5
public void Put(int id, string value)
{
}

// DELETE /api/values/5
public void Delete(int id)
{
}

When making requests using curl, JSON is the default response, even when I explicitly request XML I still get JSON:

curl http://localhost/eco/api/products/5.xml

Returns:

"http://www.google.com"

Can anyone see the problem with my setup?

The following code maps the extensions in the Global.asax file after the routes have been configured:

    GlobalConfiguration.Configuration.Formatters.JsonFormatter.
        MediaTypeMappings.Add(
            new UriPathExtensionMapping(
                "json", "application/json"
        )
    );

    GlobalConfiguration.Configuration.Formatters.XmlFormatter.
        MediaTypeMappings.Add(
            new UriPathExtensionMapping(
                "xml", "application/xml"
        )
    );
解决方案

Do you need to register the extension mapping like so:

config.Formatters.JsonFormatter.MediaTypeMappings.Add(new UriPathExtensionMapping("json", "application/json"));
config.Formatters.XmlFormatter.MediaTypeMappings.Add(new UriPathExtensionMapping("xml", "application/xml"));

Example was found here.

Update

If you look at the code for UriPathExtensionMapping the placeholder for the extension is

/// <summary>
/// The <see cref="T:System.Uri"/> path extension key.
/// </summary>
public static readonly string UriPathExtensionKey = "ext";

So your routes would need to be changed to ({ext} not {extension}):

config.Routes.MapHttpRoute(
            name: "Api UriPathExtension",
            routeTemplate: "api/{controller}.{ext}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

这篇关于UriPathExtensionMapping来控制的WebAPI响应格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 04:18