Вопрос

Now for every failure result I need to send the error message with http status code 400. how do I change the bad request to http status 400?

Here is my code:

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.
}
}
Это было полезно?

Решение

You can try this

[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;
        }
    }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top