How do I return an HTTP 400 - Bad Request error fr

2019-07-13 15:30发布

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 is this parameter us 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?

3条回答
ゆ 、 Hurt°
2楼-- · 2019-07-13 15:49

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?

查看更多
一纸荒年 Trace。
3楼-- · 2019-07-13 15:52

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.

查看更多
Bombasti
4楼-- · 2019-07-13 15:55
登录 后发表回答