我正在尝试配置 web api 区域以允许我传递可选的格式扩展名。这是我RegisterArea
的方法的代码:
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.MapHttpRoute(
name: "Api_default",
routeTemplate: @"Api/{controller}/{action}/{id}.{ext}",
defaults: new
{
action = "Index",
id = RouteParameter.Optional,
formatter = RouteParameter.Optional
},
constraints: new
{
id = @"[0-9]+",
controller = @"[^\.]+",
ext = @"json|xml"
}
);
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(
new UriPathExtensionMapping("json", "application/json")
);
GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(
new UriPathExtensionMapping("xml", "application/xml")
);
}
现在,如果我去http://www.example.com/api/countries它工作正常,但是当我尝试http://www.example.com/api/countries.json给我
No type was found that matches the controller named 'countries.json'.
我究竟做错了什么?