2013-05-04 83 views
0

我打電話從AJAX REST服務,我有以下示例呼叫如何在同一個ProcessRequest方法中使用多個處理程序?

myipaddress/RestWebService /僱員?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();     
    }    
} 
+0

對於基本的休息調用,您不必提供HttpHandlers,而是可以在ASP.Net MVC4中使用API​​控制器,然後在該控制器中使用'Redirect'。 – Saravanan 2013-05-04 12:21:51

+0

嗨,我實現了一個MVC api,就像一種享受。謝謝 – Simon 2013-05-05 12:01:00

回答

0

我不知道C#中的implementaiton但一般在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 

我希望,因爲它是一個REST約定的C#框架應提供此功能的其他約定。

相關問題