如何在postman body部分传递硬编码guid

zpgglvta  于 2023-01-05  发布在  Postman
关注(0)|答案(2)|浏览(201)

我尝试将2个guid值传递给.net API,如下图

所示
如果我通过像上面的图像我没有得到的值在.net像下面的图像..请让我知道语法来通过硬编码的guid在 Postman

k4aesqcs

k4aesqcs1#

你应该使用静态方法Guid.NewGuid()而不是调用默认的构造函数。

var ApplicationId = Guid.NewGuid();
var DistrictId = Guid.NewGuid();
kqlmhetl

kqlmhetl2#

JSON中没有GUID数据类型,因此不能直接使用它。
相反,您可以在模型中使用参数的数据类型"string"。
然后:

    • 溶液1**

也可以将参数定义为字符串,然后在方法中将它们转换为GUID:

public class AuthenticateModel 
{
    //...
    public string ApplicationId { get; set; }

    public string DistrictId { get; set; }
    //...
}

在您的方法中:

public SecurityToken AuthenticateUser(AuthenticateModel authenticateModel)
{
    var applicationId = Guid.Parse(authenticateModel.ApplicationId);
    var districtId = Guid.Parse(authenticateModel.DistrictId);
}
    • 解决方案2:**

您可以创建2个新变量:

public class AuthenticateModel 
{
    public string ApplicationId { get; set; }

    public string DistrictId { get; set; }

    [JsonIgnore] //this is for Newtonsoft.Json
    [IgnoreDataMember] //this is for default JavaScriptSerializer class
    public Guid ApplicationGuid { get => Guid.Parse(ApplicationId); set => ApplicationId = value.ToString(); }

    [JsonIgnore] //this is for Newtonsoft.Json
    [IgnoreDataMember] //this is for default JavaScriptSerializer class
    public Guid DistrictGuid { get => Guid.Parse(DistrictId); set => DistrictId = value.ToString(); }
}

然后在你的方法中使用它

public SecurityToken AuthenticateUser(AuthenticateModel authenticateModel)
{
    //...
    doSomething(authenticateModel.ApplicationGuid);
    doSomething(authenticateModel.DistrictGuid);
    //...
}

希望对你有用。

相关问题