0

我正在从 ajax 调用 REST 服务,我有以下示例调用

myipaddress/RestWebService/employee?id=1",

c# 服务代码如下所示。我上面的处理程序是“员工”,我希望添加更多处理程序,并且想知道我是否可以从相同的 ProcessRequest 方法中执行此操作,我想解析处理程序,然后根据需要使用参数引导请求,

所以我想打个电话

myipaddress/RestWebService/company?id=1",

非常感谢

void IHttpHandler.ProcessRequest(HttpContext context)
{
    try
    {                
        string url = Convert.ToString(context.Request.Url);
        connString = @"";
        dal = new DAL.DAL(connString);
        errHandler = new ErrorHandler.ErrorHandler();

        //Handling CRUD
        switch (context.Request.HttpMethod)
        {
            case "GET":
                //Perform READ Operation                   
                READ(context);
                break;
            case "POST":
                //Perform CREATE Operation
                CREATE(context);
                break;
            case "PUT":
               //Perform UPDATE Operation
                UPDATE(context);
                break;
            case "DELETE":
                //Perform DELETE Operation
                DELETE(context);
                break;
            default:
                break;
        }
    }
    catch (Exception ex)
    {
        errHandler.ErrorMessage = ex.Message.ToString();
        context.Response.Write(errHandler.ErrorMessage);                
    }
}


/// <param name="context"></param>
private void READ( HttpContext context)
{
    try
    {
        int employeeCode = Convert.ToInt16(context.Request["id"]);

        //HTTP Request Type - GET"
        //Performing Operation - READ"
        //Data sent via query string
        //POST - Data sent as name value pair and resides in the <form section> of the browser
        emp = dal.GetEmployee(employeeCode);
        if (emp==null)               
            context.Response.Write(employeeCode + "No Employee Found");

        string serializedEmployee = Serialize(emp);

        context.Response.ContentType = "text/xml";

        //string serializedEmployee = JsonSerialize(emp);
        //context.Response.ContentType = "text/json";
        WriteResponse(serializedEmployee);
    }
    catch (Exception ex)
    {
        WriteResponse("Error in READ");
        errHandler.ErrorMessage = dal.GetException();
        errHandler.ErrorMessage = ex.Message.ToString();                
    }            
}
4

1 回答 1

0

我不确定 C# 中的实现,但通常在 java REST 框架中,您可以有不同的路径参数值,由方法处理。这是它的其余约定

myipaddress/RestWebService/{entity}?id=1
This at run time can cater to both type of request

myipaddress/RestWebService/employee?id=1
myipaddress/RestWebService/company?id=1

我希望您的 c# 框架应该提供此功能,因为它是 REST 约定。

于 2013-05-05T05:41:03.823 回答