我正在从 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();
}
}