HttpWebRequest DELETE(ADO.NET 数据服务框架)

通过使用 HTTP DELETE 请求可以从 ADO.NET 数据服务中删除数据。可将 DELETE 请求应用于键、属性值、链接和导航属性标识的实体集。

用于删除实体的 DELETE 请求

DELETE 请求所需的数据的格式将设置为字符串,此字符串将成为 HTTP 请求正文的一部分。DELETE 请求首先标识实体集和标识键。下面的示例基于 AdventureWorks 销售模型 (EDM)。此 URI 包括 Address 实体集和键:"https://localhost:50781/AdvWksSalesS.svc/Address(32523)". 如果要删除的实体是继承层次结构的一部分,则 __metadata 语法元素是必需的:

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

本说明结尾处的代码块中演示了一个完整示例,此示例使用 JSON 格式(ADO.NET 数据服务框架)Address 实体集中删除一个数据项。

本说明结尾处的代码块中的代码将从部署 AdventureWorksModel 的数据服务中删除一个 Address 实体。使用要删除的实体类型的 URI 创建 HttpWebRequest rhttps://localhost:50781/AdvWksSalesS.svc/Address(32523(作为其构造函数的单个参数)。

请求的正文将分配给名为 requestPayload 的字符串。方法将设置为 "DELETE"ContentType 将分配给 json 协议。r.Accept = "application/json" 行告知服务器发送回使用 json 协议编码的响应(如果有)。

如果利用基于传输的身份验证方案(如 HTTP 基本身份验证)对服务进行保护,则可以通过将凭据分配给请求的方式来传递凭据。在下面的示例中,将使用 DefaultCredentials

请求 r 的格式将设置为 Unicode 文本。UTF8Encoding 变量用于获取请求的长度(以字节为单位),以便将数字数据写入请求的 Stream 对象中。通过为请求调用 GetResponse 来分配 HttpWebResponse 对象。代码 r.GetResponse 将发送数据并获取响应。另一个 Stream 对象 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 数据服务框架)