问题描述
我如何在MVC4使用UriPathExtensionMapping?我已经添加了映射格式化这样:
How do I use UriPathExtensionMapping in MVC4? I've added the mapping in the formatter such that:
MediaTypeMappings.Add(new UriPathExtensionMapping("json", new MediaTypeHeaderValue("application/json"))
但除非我加一个动词,比如我不能用我的路线上的扩展:
But I can't use the extension on my route unless I add a verb, such as:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}.{extension}"
);
然后,它会承认我需要的介质类型,但它也开始期待扩展名为行动上的参数。例如:
Then it'll recognize my desired media type, but it also starts expecting "extension" as a parameter on the action. Example:
public object Get(string extension)
{
}
而不是只:
public object Get()
{
}
我怎样才能解决这个问题?
How can I resolve this?
谢谢!
推荐答案
我不记得我是否注意到了这个问题,但也可能是因为我明确指定为扩展名,如默认值:
I don't remember if I noticed this problem, but it might be because I explicitly specified the default value for the extension like:
var rF = routes.MapHttpRoute(
name: "DefaultApi.v1.format",
routeTemplate: "api/1/{controller}/{action}.{format}/{*id}",
defaults: new { id = RouteParameter.Optional, format = "json", version = 1 }
);
*注格式=json的
与默认开头的行
。
我确实也注意到,这有时会不会对尾随参数请求 ID
,工作就像〜/ API / 1 /价值/ get.json / 4
。
I did also notice that this sometimes doesn't work on requests with trailing parameters for id
, like ~/api/1/values/get.json/4
.
这篇关于UriPathExtensionMapping在MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!