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!"}));
Sign up to request clarification or add additional context in comments.

4 Comments

This time it returns as {"error":"your error message here!"} with quotes. How can I eliminate that?
Why do you need it without the quotes ?
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
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!\"}");

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.