【问题标题】:Guid Parameter is always null in Controller ActionResult控制器 ActionResult 中的 Guid 参数始终为空
【发布时间】:2016-07-25 12:34:00
【问题描述】:

我有一个如下控制器:

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 个值具有来自 URL 的值,但 entitlementid 始终是 null。 可能是什么问题?

路由配置

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

【问题讨论】:

  • @KartikeyaKhosla 我根本没有
  • 这看起来像一个 WebAPI 调用。你能显示路线配置吗?
  • @Nkosi config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "{controller}/{action}/{id}", 默认值: new { id = RouteParameter.Optional } );
  • 如果您尝试将entitlementId 更改为字符串,它仍然为空吗?

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


【解决方案1】:

您在查询字符串的末尾包含一个额外的斜杠 /

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

这导致Guid 绑定无效。如果您删除斜线并发出请求,则将填充 entitlementid

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

应该按预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    相关资源
    最近更新 更多