C# 在处理json方法时,如何处理未找到的记录

C# 在处理json方法时,如何处理未找到的记录,c#,web-services,C#,Web Services,我正在使用以下web方法为驱动程序获取数据,但我的问题是。我的问题是,如果没有找到数据,返回json处理错误的最佳方法是什么?具体的错误代码是PageNotFound i no is 404,我应该在我的webmethod中返回 [WebMethod] [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] public motorista GetDriverInformation(int driverId

我正在使用以下web方法为驱动程序获取数据,但我的问题是。我的问题是,如果没有找到数据,返回json处理错误的最佳方法是什么?具体的错误代码是PageNotFound i no is 404,我应该在我的webmethod中返回

[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] 
public motorista GetDriverInformation(int driverId)
{

        motorista _wsmoristita = new motorista();
        _wsmoristita = _dal.GetDriverInformation(driverId);
        return _wsmoristita;          

}

我想您正在使用jqueryajax。您可以在带有特定消息的
WebMethod
中抛出自定义异常,然后在ajax调用中定义的错误函数中显示此消息

$.ajax({})的一部分

您的
OnErrorCall
函数如下所示

 function OnErrorCall(response) {

     alert(response.responseText);
  }
您甚至可以在某些文本框中显示消息,因此,如果您抛出异常,并且文本未找到记录或类似内容,则可以在标签、警报等中显示该消息

下面是一个自定义异常的示例。你想看就看

public class EmployeeListNotFoundException: Exception
{
    public EmployeeListNotFoundException()
    {
    }

    public EmployeeListNotFoundException(string message)
        : base(message)
    {
    }

    public EmployeeListNotFoundException(string message, Exception inner)
        : base(message, inner)
    {
    }
}

我想您正在使用jqueryajax。您可以在带有特定消息的
WebMethod
中抛出自定义异常,然后在ajax调用中定义的错误函数中显示此消息

$.ajax({})的一部分

您的
OnErrorCall
函数如下所示

 function OnErrorCall(response) {

     alert(response.responseText);
  }
您甚至可以在某些文本框中显示消息,因此,如果您抛出异常,并且文本未找到记录或类似内容,则可以在标签、警报等中显示该消息

下面是一个自定义异常的示例。你想看就看

public class EmployeeListNotFoundException: Exception
{
    public EmployeeListNotFoundException()
    {
    }

    public EmployeeListNotFoundException(string message)
        : base(message)
    {
    }

    public EmployeeListNotFoundException(string message, Exception inner)
        : base(message, inner)
    {
    }
}

我最终使用了下面这样的方法,但我认为这并不是最好的方法,因为它是通用的,并且不会指出导致web开发人员出错的专栏

if (_wsHistory.id_terminal == 0)
 {

            Context.Response.Status = "404 Terminal table Not Found";
            //the next line is untested - thanks to strider for this line
            Context.Response.StatusCode = 404;
            //the next line can result in a ThreadAbortException
            //Context.Response.End(); 
            Context.ApplicationInstance.CompleteRequest();
            return null;
}

我最终使用了下面这样的方法,但我认为这并不是最好的方法,因为它是通用的,并且不会指出导致web开发人员出错的专栏

if (_wsHistory.id_terminal == 0)
 {

            Context.Response.Status = "404 Terminal table Not Found";
            //the next line is untested - thanks to strider for this line
            Context.Response.StatusCode = 404;
            //the next line can result in a ThreadAbortException
            //Context.Response.End(); 
            Context.ApplicationInstance.CompleteRequest();
            return null;
}