共用方式為


HttpWebRequest DELETE (ADO.NET 資料服務架構)

您可以使用 HTTP DELETE 要求,從 ADO.NET 資料服務中刪除資料。DELETE 要求可以套用至索引鍵所識別的實體集、屬性值、連結和導覽屬性。

移除實體的 DELETE 要求

DELETE 要求所需的資料會格式化成為屬於 HTTP 要求主體一部分的字串。DELETE 要求會先識別實體集和識別索引鍵。下列範例是以 AdventureWorks Sales Model (EDM) 為基礎。此 URI 包含 Address 實體集和索引鍵:"https://localhost:50781/AdvWksSalesS.svc/Address(32523)"。如果要移除的實體屬於繼承階層架構 (Inheritance Hierarchy) 的一部分,就會需要使用 __metadata 語法項目:

  "{__metadata:{Uri:'/Addres(32523/', " +
      "Type:'AdventureWorksModel.Address'}}"

使用 JSON 格式 (ADO.NET 資料服務架構)Address 實體集中移除資料項目的完整範例顯示在這段描述結尾的程式碼區塊中。

位於這段描述結尾之程式碼區塊中的程式碼會從部署 AdventureWorksModel 的資料服務中刪除 Address 實體。HttpWebRequest r 會使用要刪除之實體類型的 URI (https://localhost:50781/AdvWksSalesS.svc/Address(32523) 建立成其建構函式 (Constructor) 的單一參數。

要求的主體會指派給名為 requestPayload 的字串。方法設定為 "DELETE"ContentType 會指派給 json 通訊協定 (Protocol)。r.Accept = "application/json" 這一行會告知伺服器傳回使用 json 通訊協定所編碼的回應 (如果有的話)。

如果服務受到「HTTP 基本驗證」等以傳輸為基礎的驗證配置所保護,就可以將認證指派給要求,藉以傳遞認證。下列範例使用的是 DefaultCredentials

要求 r 會格式化成為 Unicode 文字。UTF8Encoding 變數會用於取得要求的長度 (以位元組為單位),以便將數位資料寫入要求的 Stream 物件。HttpWebResponse 物件的指派方式是針對要求呼叫 GetResponse。程式碼 r.GetResponse 會傳送資料並取得回應。另一個資料流物件 (Stream Object) rspStm 則是用於包含 GetResponseStream 所傳回的資料。

    HttpWebRequest r =
  WebRequest.Create(
            "https://localhost:50781/AdvWksSalesS.svc/Address(32523)")
            as HttpWebRequest;

    string requestPayload = "{__metadata:{Uri:'/Address(32523)/', " +
        "Type:'AdventureWorksModel.Address'}}";

    r.Method = "DELETE";
    UTF8Encoding encoding = new UTF8Encoding();
    r.ContentLength = encoding.GetByteCount(requestPayload);
    r.Credentials = CredentialCache.DefaultCredentials;
    r.Accept = "application/json";
    r.ContentType = "application/json";

    //Write the payload to the request body.
    using ( Stream requestStream = r.GetRequestStream())
    {
        requestStream.Write(encoding.GetBytes(requestPayload), 0,
            encoding.GetByteCount(requestPayload));
    }

    try
    {
        HttpWebResponse response = r.GetResponse() as HttpWebResponse;
        string responseBody = "";
        using (Stream rspStm = response.GetResponseStream())
        {
            using (StreamReader reader = new StreamReader(rspStm))
            {
                textBoxResponse.Text = textBoxResponse.Text + 
                    "Response Description: " + 
                     response.StatusDescription;
                textBoxResponse.Text = textBoxResponse.Text + 
                    "Response Status Code: " + response.StatusCode;
                textBoxResponse.Text = textBoxResponse.Text +
                    "\r\n\r\n";
                responseBody = reader.ReadToEnd();
            }
        }
        textBoxResponse.Text = "Success: " + response.StatusCode.ToString();
    }
    catch (System.Net.WebException ex)
    {
        textBoxResponse.Text = textBoxResponse.Text + 
            "Exception message: " + ex.Message;
        textBoxResponse.Text = textBoxResponse.Text + 
            "\r\nResponse Status Code: " + ex.Status;
        textBoxResponse.Text = textBoxResponse.Text + "\r\n\r\n";
        // get error details sent from the server
        StreamReader reader = 
            new StreamReader(ex.Response.GetResponseStream());
        textBoxResponse.Text = textBoxResponse.Text + reader.ReadToEnd();
        
    }

如需搭配 DELETE 方法使用之通訊協定的詳細資訊,請參閱DELETE 方法 (ADO.NET 資料服務架構)

另請參閱

概念

DELETE 方法 (ADO.NET 資料服務架構)
HttpWebRequest GET (ADO.NET 資料服務架構)
HttpWebRequest PUT (ADO.NET 資料服務架構)
HttpWebRequest POST (ADO.NET 資料服務架構)
PUT、POST 和 DELETE (ADO.NET 資料服務架構)