Controller ActionResult中的Guid参数始终为null

时间:2016-07-25 12:34:01

标签: c# asp.net-mvc asp.net-web-api controller

我有一个如下控制器:

public async Task<IHttpActionResult> MyControllerMethod(string currency = null,
                                                            string edition = null,
                                                            int? systems = null,
                                                            string version = null,
                                                            Guid? entitlementid = null)
{
//Code here
}

当我从此URL执行此控制器时:

http://*:*/MyController/MyControllerMethod/?currency=eur&edition=DSSTANDARD&systems=50&version=6.3/

方法的所有参数都具有如下值:

currency = eur
edition = DSSTANDARD
systems = 50
version = 6.3

但是,如果我这样做,添加最后一个参数:

...&entitlementid=B5630B37-0820-4EB0-8A2A-000C44885590/

然后,前3个值包含来自网址的值,但entitlementid始终为null。 可能是什么问题?

路线配置

config.Routes.MapHttpRoute( 
    name: "DefaultApi", 
    routeTemplate: "{controller}/{action}/{id}", 
    defaults: new { id = RouteParameter.Optional } 
);

1 个答案:

答案 0 :(得分:1)

您在查询字符串

的末尾添加了一个额外的斜杠/
...&entitlementid=B5630B37-0820-4EB0-8A2A-000C44885590/

导致Guid绑定失效。如果您删除斜杠并发出请求,则会填充entitlementid

http://*:*/MyController/MyControllerMethod/?currency=eur&edition=DSSTANDARD&systems=50&version=6.3&entitlementid=B5630B37-0820-4EB0-8A2A-000C44885590

应该按预期工作。

相关问题