How do I return an HTTP 400 - Bad Request error from a web form request?
Asked Answered
C

3

7

The subject pretty much says it all. I have an EstateReport web form that must be called with an EstateId request parameter. I want to return an appropriate HTTP error if this parameter is not present. How do I return an HTTP error 400 as my response?

On a tangent, should I return an error if the required parameter isn't present, which I feel is more correct, or redirect to the search page for the report, which is more user friendly?

Coquelicot answered 23/8, 2011 at 19:23 Comment(0)
J
3

So from what I understand the form requires a parameter to display results in a meaningful way. A 400 is a Bad Request, while I understand your thinking, the specification states:

"The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications."

In its purest sense, if someone requests say "EstateReport.aspx" without a parameter, the server can still interpret this and reply with an appropriate response, so its not a "bad request" in its broadest sense.

What I would suggest is you detect the absence of the parameter and render an appropriate error message with a link to a page where they could select an appropriate "estateId" via some way, so when EstateReport is requested, the parameter is present.

Jaynajayne answered 23/8, 2011 at 19:30 Comment(3)
Thanks for the input @kd7, but I am also considering the report URL being requested by a machine, so an error message should be machine readable, and a status code is machine readable.Coquelicot
Ahh I see... I only thought humans would be requesting the page via a browser.Jaynajayne
So far it will be only humans, but I am often guilty of thinking ahead too much. :-) Could well be that a service requests a report, then publishes a PDF version of it to subscribers. I wouldn't want them all getting emailed a PDF version of an error message.Coquelicot
S
32

you can:

throw new HttpException(400, "Bad Request");

or

Response.StatusCode = 400;
Response.End();

but I'm with the same thinking as kd7 below - why not display an error message to the client letting them know what is wrong?

Siderolite answered 23/8, 2011 at 19:32 Comment(2)
I had to use new instead of New before it would compile.Antilogy
@Antilogy It took eight years for this typo to be detected and then another month to be edited :-)Pinnacle
J
3

So from what I understand the form requires a parameter to display results in a meaningful way. A 400 is a Bad Request, while I understand your thinking, the specification states:

"The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications."

In its purest sense, if someone requests say "EstateReport.aspx" without a parameter, the server can still interpret this and reply with an appropriate response, so its not a "bad request" in its broadest sense.

What I would suggest is you detect the absence of the parameter and render an appropriate error message with a link to a page where they could select an appropriate "estateId" via some way, so when EstateReport is requested, the parameter is present.

Jaynajayne answered 23/8, 2011 at 19:30 Comment(3)
Thanks for the input @kd7, but I am also considering the report URL being requested by a machine, so an error message should be machine readable, and a status code is machine readable.Coquelicot
Ahh I see... I only thought humans would be requesting the page via a browser.Jaynajayne
So far it will be only humans, but I am often guilty of thinking ahead too much. :-) Could well be that a service requests a report, then publishes a PDF version of it to subscribers. I wouldn't want them all getting emailed a PDF version of an error message.Coquelicot
R
1

Change the HttpResponse.Status property.

Details: http://msdn.microsoft.com/en-us/library/system.web.httpresponse.status(v=vs.71).aspx

Rocher answered 23/8, 2011 at 19:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.