开发者

Is it possible to manually send HTTP code 40x to client from ASP.NET Web service method?

开发者 https://www.devze.com 2023-02-03 22:07 出处:网络
I would like to manually send an HTTP 40x error code to client from a web method in c# asp.net web service.

I would like to manually send an HTTP 40x error code to client from a web method in c# asp.net web service.

Specifically I need to send 400 (Bad request) or 401 (Unauthorized).

I was thinking about the flow to look like:

if (a <= 0)
{
   //send HTTP Error 400 - Bad request to client
   return;
}

Is it possible? 开发者_如何转开发How?


Set the response status code

Response.Clear();
Response.Statuscode = 404; //Your response here
Response.End;

I believe its important to clear the response first in case it is already partially populated

0

精彩评论

暂无评论...
验证码 换一张
取 消