English 中文(简体)
WCF REST = 。 方法 = 删除(403)
原标题:WCF REST => Method = Delete => (403) Forbidden

I m 从事GET、POST和DELETE业务,来自我的WCF REST服务。 虽然GET & POST运行良好,但DELETE没有发挥作用。

From my client application, when i try to call delete it says below error. Error Message: The remote server returned an error: (403) Forbidden.

<><>My Code:

<>Data合同:

[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;
}

www.un.org/Depts/DGACM/index_spanish.htm 消费法:

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;
}

Could you please point me where I m wrong?

问题回答

I d 尝试《全民公决、所有礼》或其他选择:

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;
        }
    }

我最近似乎有同样的问题,这是由于WebDAV拦截了这些要求。 取消这一应用模块解决了这一问题。

人口调查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>




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签