0

我正在从我的 WCF REST 服务执行 GET、POST 和 DELETE 操作。虽然 GET & POST 运行良好,但 DELETE 不起作用。

在我的客户端应用程序中,当我尝试调用 delete 时,它​​会显示以下错误。 错误消息:远程服务器返回错误:(403) Forbidden。

我的代码:

数据合约:

[WebInvoke(Method = "DELETE", UriTemplate = "users/", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
string DeleteUser(UserInfo userinfo);

执行:

public string DeleteUser(UserInfo userinfo)
{
    string sResult = "";

    try
    {
        UserDataAccess dataAccess = new UserDataAccess();

        sResult = dataAccess.DeleteUser(userinfo.GEId);
    }
    catch (Exception ex)
    {
        oLog.LogError(ex, true);
        sResult = ex.Message;
    }

    return sResult;
}

消费代码:

protected void btDeleteAsJson_Click(object sender, EventArgs e)
{
    UserInfo user = new UserInfo();

    user.GEId = Convert.ToInt32(txtGEID.Text);

    JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
    string output = jsSerializer.Serialize(user);

    SendMessageDeleteJson(apiUrl, output);
}

public void SendMessageDeleteJson(string endPoint, string output)
{
    string data = output;

    byte[] bytes = Encoding.UTF8.GetBytes(data);

    HttpWebRequest request = CreateWebRequest1(endPoint, bytes.Length, "application/json");

    using (var requestStream = request.GetRequestStream())
    {
        requestStream.Write(bytes, 0, bytes.Length);
    }

    using (var response = (HttpWebResponse)request.GetResponse())
    {
        if (response.StatusCode != HttpStatusCode.OK)
        {
            string message = String.Format("DELETE failed. Received HTTP {0}", response.StatusCode);
            throw new ApplicationException(message);
        }

        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        string sResponseStream = reader.ReadToEnd();

        lblDeleteSuccess.Text = "Success! User has beed deleted";
    }
}

private HttpWebRequest CreateWebRequest1(string endPoint, Int32 contentLength, string ContentType)
{
    var request = (HttpWebRequest)WebRequest.Create(endPoint);

    request.Method = "DELETE";
    request.ContentLength = contentLength;
    //request.ContentType = "application/x-www-form-urlencoded";
    request.ContentType = ContentType;

    return request;
}

你能指出我错在哪里吗?

4

2 回答 2

1

我会尝试将 SetEntitySetAccessRule 设置为 AllRead、AllWrite 或其他选项:

public class WcfDataService1 : DataService< HenryContext >
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)

        {            

              config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);

             config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
        }
    }
于 2013-04-20T22:39:49.970 回答
0

我最近遇到了同样的问题,这是由于 WebDAV 拦截了请求。删除该应用程序的模块解决了这个问题。

在 IIS 7 上:

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <remove name="WebDAVModule" />
    <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </modules>
</system.webServer>

在 IIS 6 上:

<system.web>
  <httpModules>
    <remove name="WebDAVModule" />
    <!-- ... -->
  </httpModules>
</system.web>
于 2011-11-22T11:25:00.497 回答