簡體   English   中英

如何在 postman 正文部分傳遞硬編碼 guid

[英]how to pass hardcoded guid in postman body section

我正在嘗試將 2 個 guid 值傳遞給 .net api,如下圖所示在此處輸入圖像描述

如果我像上圖一樣通過,我不會像下圖那樣獲得 .net 中的值。請告訴我在 postman 中傳遞硬編碼 guid 的語法

在此處輸入圖像描述

您應該使用 static 方法Guid.NewGuid()而不是調用默認構造函數。 這應該有效:

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

JSON中沒有GUID數據類型,不能直接使用。

相反,您可以在 model 中將參數的數據類型用作“字符串”。

然后:

解決方案 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);
    //...
}

希望對你有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM