我有越来越UriPathExtensionMapping问题在ASP.NET的WebAPI工作。 我的设置如下:
我的路线是:
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 }
);
我的全球ASAX文件:
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
我的控制器是:
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)
{
}
制作时使用curl请求,JSON是默认的响应,甚至当我明确要求XML我仍然得到JSON:
curl http://localhost/eco/api/products/5.xml
返回:
"http://www.google.com"
任何人都可以看到我的设置问题?
下面的代码映射在Global.asax文件扩展名的路线已配置完成后:
GlobalConfiguration.Configuration.Formatters.JsonFormatter.
MediaTypeMappings.Add(
new UriPathExtensionMapping(
"json", "application/json"
)
);
GlobalConfiguration.Configuration.Formatters.XmlFormatter.
MediaTypeMappings.Add(
new UriPathExtensionMapping(
"xml", "application/xml"
)
);