By IngTun2018


2020-01-14 13:19:22 8 Comments

I have a Wcf Rest service

    [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]       
    void Import(stringrequest);

My try code:

 public void Import(string request)
    {
        if (request != null)
        {
             //....
        }
        else
        {
            throw new ApplicationException("Empty DATA");
        }
    }

In fact, I want to display a specific error message on the else treatment,when I test my wcf srevice in POSTMAN , if I enter a empty string ---> My objectif to display "Empty DATA",

How can do that? Thanks,

1 comments

@Philippe B. 2020-01-14 13:57:12

If you want to handle exception you should use try-catch.

When you want to handle all possible behavior, like empty strings or string full of numbers etc.. You can use if-else statement as you are doing.

You need to change your return type from void to string :

public string Import(string request) {

    if (String.IsNullOrEmpty(request)) {
        // ...
        return "{ \"Status\" : \"Ok\" }"; // or null if you don't want to return anything
    }
    else 
    {
        return "{ \"Status\" : \"Error : Empty DATA\" }";    
    }
}

You can also check this link to learn more about how to use Json in C#. How to use http return code and how to return Json, that could help you understand a bit more how WCF REST works.

Hope this helps.

Related Questions

Sponsored Content

8 Answered Questions

[SOLVED] Manually raising (throwing) an exception in Python

  • 2010-01-12 21:07:40
  • TIMEX
  • 1789008 View
  • 2311 Score
  • 8 Answer
  • Tags:   python exception

34 Answered Questions

[SOLVED] How do you assert that a certain exception is thrown in JUnit 4 tests?

25 Answered Questions

[SOLVED] How can I safely create a nested directory?

11 Answered Questions

[SOLVED] What are the correct version numbers for C#?

5 Answered Questions

[SOLVED] Catch multiple exceptions in one line (except block)

34 Answered Questions

[SOLVED] What exactly is RESTful programming?

  • 2009-03-22 14:45:39
  • hasen
  • 1660099 View
  • 3993 Score
  • 34 Answer
  • Tags:   http rest definition

28 Answered Questions

[SOLVED] Catch multiple exceptions at once?

24 Answered Questions

[SOLVED] How do I POST JSON data with cURL?

64 Answered Questions

[SOLVED] What is the difference between String and string in C#?

34 Answered Questions

[SOLVED] PUT vs. POST in REST

  • 2009-03-10 14:25:20
  • alex
  • 2404707 View
  • 5427 Score
  • 34 Answer
  • Tags:   http rest post put

Sponsored Content