我从ajax调用REST服务,我有以下示例调用
myipaddress / RestWebService /雇员?ID = 1" ,
c#服务代码如下所示。我上面的处理程序是“employee”,我希望添加更多处理程序,并且想知道我可以从同一个ProcessRequest方法执行此操作,我想解析处理程序,然后根据需要使用参数指示请求,
所以我希望有一个像
这样的电话myipaddress / RestWebService /公司?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();
}
}
答案 0 :(得分: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约定。