简体   繁体   English

属性路由-未解析枚举

[英]Attribute routing - Enum not parsed

I'm trying to make a route, but it's not working in this case: 我正在尝试创建一条路线,但在这种情况下不起作用:

If I call: 如果我打电话给:

http://mysite.com/api/v1/product/Generic/1A 

works fine. 工作正常。

If I call: 如果我打电话给:

http://mysite.com/api/v1/product?=Generic 

works too, but when I call: 也可以,但是当我打电话时:

http://mysite.com/api/v1/product/Generic 

I get this error: 我收到此错误:

{
 "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."
}

The code of my route: 我的路线代码:

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,
}

And the Controller: 和控制器:

public HttpResponseMessage Get(GType type)
{
    ...
}

public HttpResponseMessage Get(GType type, string id)
{
    ...
}

So, Web API isn't parsing the value in the URL. 因此,Web API不会解析URL中的值。 Did I forget anything? 我忘记了什么吗?

Well, I still can't see how your first url actually works, but the issue with the second url is that you are trying to pass an invalid bit of data as a parameter. 嗯,我仍然看不到您的第一个URL实际如何工作,但是第二个URL的问题是您试图将无效的数据位作为参数传递。

To keep it simple, I'm just going to pretend for a minute that your route definition is this: 为了简单起见,我仅假装一分钟,您的路由定义是这样的:

 routeTemplate: "api/{controller}/{type}/{id}",
 defaults: new { id = RouteParameter.Optional }

If you were to call a GET on this url http://mysite.com/product/Generic , then you will get the same error you are running into. 如果您要在此URL http://mysite.com/product/Generic上调用GET,那么您将遇到遇到的相同错误。 This url will resolve a controller of ProductController , with a parameter called type that will have a value of Generic . 此网址将解析ProductController的控制器,其参数名为type的值将为Generic

However, your type parameter has an actual Type of GType , which is an enum. 但是,您的type参数具有一个实际的GType Type,它是一个枚举。 Generic is not a valid value for that so an error occurs. 通用不是有效值,因此会发生错误。 It's the same as sending "abcd" as the value for a parameter that is an int . 这与发送“ abcd”作为int参数的值相同。

If you tried to call a get to http://mysite.com/product/Big , it would work, as it would be able to parse that value (Big) and a member of the GType enum. 如果您尝试调用http://mysite.com/product/Big的get,它将可以工作,因为它可以解析该值(Big)和GType枚举的成员。

The problem was that I have two equals routes, but with diferents parameters name. 问题是我有两个等值路由,但参数名称不同。

api/{version}/{controller}/{type}/{id} 

and

api/{version}/{controller}/{id}

The second must be the last route declared. 第二个必须是声明的最后一条路线。

The route you show us doesn't remotely match the URLs you're trying to hit. 您显示给我们的路线与您尝试访问的URL远程匹配。 I have a feeling you're looking at the wrong route. 我觉得您在寻找错误的路线。 Also, shouldn't "/Product/" be " /{controller}/" ? 另外, "/Product/"不应该是“ /{controller}/"吗?

Oh, and you mis-spelled routeTamplate [sic] in the snippet. 哦,您在代码段中拼写了routeTamplate [sic]。 I assume that was a transcription error -- I'm pretty sure the compiler would bark on that. 我认为那是一个转录错误-我很确定编译器会对此进行吠叫。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM