我正在尝试制定路线,但在这种情况下不起作用:
如果我打电话:
http://mysite.com/api/v1/product/Generic/1A
工作正常。
如果我打电话:
http://mysite.com/api/v1/product?=Generic
也可以,但是当我打电话时:
http://mysite.com/api/v1/product/Generic
我收到此错误:
{
"Message":"The request is invalid.",
"MessageDetail":"The parameters dictionary contains a null entry for parameter 'type'
of non-nullable type 'GType' for method 'System.Net.Http.HttpResponseMessage
Get(GType)' in 'MySite.ControllersApi.V2.ProductController'. An optional parameter must
be a reference type, a nullable type, or be declared as an optional parameter."
}
我的路线代码:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute
(
name: "DefaultApi",
routeTemplate: "api/{version}/{controller}/{type}/{id}",
defaults: new { id = RouteParameter.Optional, version = "v1", controller = "Product" }
)
}
}
public enum GType
{
Big,
Small,
Generic,
}
和控制器:
public HttpResponseMessage Get(GType type)
{
...
}
public HttpResponseMessage Get(GType type, string id)
{
...
}
因此,Web API 不会解析 URL 中的值。我忘了什么吗?