Share via

Clean NULL condition

Anonymous
2021-11-30T22:14:36.56+00:00

Hi,

I have below code for sending push notifications using REST API

var client = new HttpClient();
client.BaseAddress = new Uri("https://onesignal.com/api/v1/notifications");
client.DefaultRequestHeaders.Add("Authorization", "Basic " + SecureStorage.GetAsync("AppAuthID").Result);

var content = new StringContent(JsonSerializer.Serialize(new
{
    app_id = SecureStorage.GetAsync("AppID").Result,
    headings = new
    {
        en = TextBoxNewMessageTitle.Text.Trim()
    },
    subtitle = new
    {
        en = TextBoxNewMessageSubtitle.Text.Trim()
    },
    contents = new
    {
        en = EditorMessage.Text.Trim()
    },
    url = "https://www.cnn.com",
    included_segments = "All",
}), Encoding.UTF8, "application/json");

I want to do a clean NULL condition to check the values before adding it. For example if TextBoxNewMessageTitle.Text isNullOrWhiteSpace then no need to add the headings at all because it should only be added when the TextBoxNewMessageTitle. has a value

Kindly help..

Thanks,
Jassim

Developer technologies | .NET | Xamarin
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Viorel 127K Reputation points
    2021-12-02T07:02:31.807+00:00

    Try something like this:

    var data = new
     {
         app_id = SecureStorage.GetAsync("AppID").Result,
         headings = string.IsNullOrWhiteSpace(TextBoxNewMessageTitle.Text) ? null : new
         {
             en = TextBoxNewMessageTitle.Text.Trim()
         },
         subtitle = string.IsNullOrWhiteSpace(TextBoxNewMessageSubtitle.Text) ? null :  new
         {
             en = TextBoxNewMessageSubtitle.Text.Trim()
         },
         contents = string.IsNullOrWhiteSpace(EditorMessage.Text) ? null : new
         {
             en = EditorMessage.Text.Trim()
         },
         url = "https://www.cnn.com",
         included_segments = "All",
    };
    
    var o = new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull };
    string json = JsonSerializer.Serialize( data, o);
    
    var content = new StringContent( json, Encoding.UTF8, "application/json");
    

    Was this answer helpful?

    1 person found this answer helpful.

  2. JarvanZhang 23,971 Reputation points
    2021-12-01T01:36:39.487+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    For example if TextBoxNewMessageTitle.Text isNullOrWhiteSpace then no need to add the headings at all

    You could use string.IsNullOrWhiteSpace method to check if the text is null, empty, or consists only of white-space characters.

       if (!string.IsNullOrWhiteSpace(TextBoxNewMessageTitle.Text))  
       {  
           ...  
           headings = new  
           {  
               en = TextBoxNewMessageTitle.Text.Trim()  
           }  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.