C# 如何在同一ProcessRequest方法中有多个处理程序?
我正在从ajax调用REST服务,我有以下示例调用 myipaddress/RestWebService/employee?id=1“ c#服务代码如下所示。我的处理程序如上所示为“employee”,我希望添加更多处理程序,并想知道是否可以从相同的ProcessRequest方法执行此操作,我希望解析处理程序,然后根据需要使用参数引导请求 所以我想打个电话 myipaddress/RestWebService/company?id=1“ 非常感谢C# 如何在同一ProcessRequest方法中有多个处理程序?,c#,ajax,wcf,rest,C#,Ajax,Wcf,Rest,我正在从ajax调用REST服务,我有以下示例调用 myipaddress/RestWebService/employee?id=1“ c#服务代码如下所示。我的处理程序如上所示为“employee”,我希望添加更多处理程序,并想知道是否可以从相同的ProcessRequest方法执行此操作,我希望解析处理程序,然后根据需要使用参数引导请求 所以我想打个电话 myipaddress/RestWebService/company?id=1“ 非常感谢 void IHttpHandler.Proce
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();
}
}
void IHttpHandler.ProcessRequest(HttpContext上下文)
{
尝试
{
字符串url=Convert.ToString(context.Request.url);
connString=@;
dal=新的dal.dal(连接字符串);
errHandler=newerrorhandler.ErrorHandler();
//处理积垢
开关(context.Request.HttpMethod)
{
案例“GET”:
//执行读取操作
阅读(上下文);
打破
案例“职位”:
//执行创建操作
创建(上下文);
打破
案例“付诸表决”:
//执行更新操作
更新(背景);
打破
案例“删除”:
//执行删除操作
删除(上下文);
打破
违约:
打破
}
}
捕获(例外情况除外)
{
errHandler.erromessage=ex.Message.ToString();
context.Response.Write(errHandler.ErrorMessage);
}
}
///
私有无效读取(HttpContext上下文)
{
尝试
{
int employeeCode=Convert.ToInt16(context.Request[“id”]);
//HTTP请求类型-获取“
//正在执行操作-读取“
//通过查询字符串发送的数据
//POST-数据作为名称-值对发送,并驻留在浏览器的
emp=dal.GetEmployee(员工代码);
如果(emp==null)
context.Response.Write(employeeCode+“未找到员工”);
string serializedEmployee=Serialize(emp);
context.Response.ContentType=“text/xml”;
//字符串serializedEmployee=JsonSerialize(emp);
//context.Response.ContentType=“text/json”;
书面答复(被解雇者);
}
捕获(例外情况除外)
{
写入响应(“读取错误”);
errHandler.ErrorMessage=dal.GetException();
errHandler.erromessage=ex.Message.ToString();
}
}
我不确定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约定。对于基本REST调用,您不必提供HttpHandlers,相反,您可以在ASP.Net MVC4中使用API控制器,然后在该控制器中使用
重定向
。您好,我实现了一个MVC API,工作起来很轻松。谢谢