C# 如何在SOAP web中发送http默认状态代码400?

C# 如何在SOAP web中发送http默认状态代码400?,c#,soap,C#,Soap,现在,对于每个失败结果,我需要发送带有http状态代码400的错误消息。如何将错误请求更改为http状态400 这是我的密码: public class Exams : System.Web.Services.WebService { [WebMethod] public string addexam(string cust_name) { if (cust_name=="") { return "Invalid request"; } BECo

现在,对于每个失败结果,我需要发送带有http状态代码400的错误消息。如何将错误请求更改为http状态400

这是我的密码:

public class Exams : System.Web.Services.WebService
{

[WebMethod]
public string addexam(string cust_name)
{
    if (cust_name=="")
    {
        return "Invalid request";
    }
    BECommon objBECommon = new BECommon();
    objBECommon.cust_name = cust_name;

    string result = objBECommon.DsResult.Tables[0].Rows[0][0].ToString();
    if (result == "1")
    {
        return "Customer exists";
    }
    else 
    {
        return "invalid request";
    }
 // here i need to send the above error message with http status code 400.
}
}
你可以试试这个

[WebService(Namespace = "http://example.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
[System.ComponentModel.ToolboxItem(false)]
public class Service: System.Web.Services.WebService

{

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public Result GetData()
    {
        User user = GetUser();

        if (user.LoggedIn)
        {
            return GetData();
        }
        else
        {
            Context.Response.Status = "403 Forbidden"; 
            //the next line is untested - thanks to strider for this line
            Context.Response.StatusCode = 403;
            Context.Response.End(); 
            return null;
        }
    }

如果你抄写了答案,请礼貌地告诉我。甚至更好的是,将问题标记为重复关闭。