0

I am using a JavaScript plug-in and if there is an error it expects format like;

{error: 'You are not allowed to upload such a file.'}

In my MVC Web API I am throwing error like;

 var error = string.Format("An error has been occured. Please try again later.");
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, error));

and it is represented in HTTP response like below;

{"Message":"An error has been occured. Please try again later."}

how can I achieve to return in first way?

1 Answer 1

2

You can create an anonymous object

throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError,new { error = "your error message here!"}));
4
  • This time it returns as {"error":"your error message here!"} with quotes. How can I eliminate that? Commented Apr 7, 2015 at 18:17
  • Why do you need it without the quotes ? Commented Apr 7, 2015 at 18:18
  • I think plug-in I am using is not accepting other than format {error: 'You are not allowed to upload such a file.'} Please see at plugins.krajee.com/file-input/demo#async-send Commented Apr 7, 2015 at 18:19
  • You can parse the JSON String to an Javascript Object in order to use it like you need it. Example: JSON.parse("{\"error\":\"your error message here!\"}"); Commented Apr 7, 2015 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.