How to allow the Post method in the http request?

11.8k views Asked by At

I am trying to create a RESTFul service with the standard HTTP verbs (GET, POST, PUT, DELETE). I use classic WCF service with RESTful annotation. My issue is that I get a "method not allowed" when requesting my service with POST.

Note: I am using IIS 8.5 asp.net

Here is my code:

Web.config:

 <customHeaders>
      <add name="Access-Control-Allow-Origin" value="http://localhost" />
      <add name="Access-Control-Allow-Headers" value="Content-Type, Accept, X-Requested-With" />
      <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE"/>
      <add name ="cache-control" value ="private, max-age=0, no-cache"/>
    </customHeaders> 

UserService.svc:

using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Runtime.Remoting.Messaging;
        using System.Runtime.Serialization;
        using System.ServiceModel;
        using System.ServiceModel.Web;
        using System.Text;

        namespace MyApp.WS
        {
        [ServiceContract]
public interface UserService
{
    //POST operation
    [OperationContract]
        [WebInvoke(Method = "POST",
                   ResponseFormat = WebMessageFormat.Json,
                   RequestFormat = WebMessageFormat.Json,
                   BodyStyle = WebMessageBodyStyle.Bare,
                   UriTemplate = "/Users")]
    UserDTO CreateUser(UserDTO createUser);

    //Get Operation
    [OperationContract]
        [WebGet(UriTemplate = "/Users", ResponseFormat = WebMessageFormat.Json)]
    List<UserDTO> GetAllUsers();

    [OperationContract]
        [WebGet(UriTemplate = "/Users/{id}", ResponseFormat = WebMessageFormat.Json)]
    UserDTO GetAUser(string id);

    //PUT Operation
    [OperationContract]
        [WebInvoke(UriTemplate = "/Users/{id}", Method = "PUT", ResponseFormat = WebMessageFormat.Json)]
    UserDTO UpdateUser(string id, UserDTO updateUser);

    //DELETE Operation
    [OperationContract]
        [WebInvoke(UriTemplate = "/Users/{id}", Method = "DELETE", ResponseFormat = WebMessageFormat.Json)]
    void DeleteUser(string id);


}

}

And UserServiceImpl.svc:

using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Runtime.Serialization;
        using System.ServiceModel;
        using System.ServiceModel.Activation;
        using System.ServiceModel.Web;
        using System.Text;
        using AutoMapper;
        using MyApp.Model;


        namespace MyApp.WS
        {
        // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
        // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
        [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class UserServiceImpl : UserService
        {

public MyApp.Controller.UserController UserControllerImpl{get;set;}


public UserDTO CreateUser(UserDTO createUser)

        {

        User user = Mapper.Map<User>(createUser);
        user = UserControllerImpl.Save(user);


        return Mapper.Map<UserDTO>(user);
        }

public List<UserDTO> GetAllUsers()
        {

        // var u = new UserDTO();
        //// u.ID = 3;
        // u.Name = "coucou";
        // u.DateCreate = DateTime.Now;

        // CreateUser(u);



        var result = UserControllerImpl.GetAll();
        return Mapper.Map <List<User>,List<UserDTO>>(result);



        }

public UserDTO GetAUser(string id)
        {
        var result = UserControllerImpl.Get(int.Parse(id));

        return Mapper.Map<UserDTO>(result);
        }

public UserDTO UpdateUser(string id, UserDTO updateUser)
        {
        updateUser.id = int.Parse(id);
        var user = Mapper.Map<User>(updateUser);
        UserControllerImpl.Update(user);

        return Mapper.Map<UserDTO>(user);

        }

public void DeleteUser(string id)
        {
        UserControllerImpl.Delete(int.Parse(id));
        }


        }
        }

normally, in my web.config with this customheader I would like have the permission to send a post ajax $

Actually I have request GET it's good but not good for the request POST

I don't understand good when the server send this error message it's possible to help me ? thanks

2

There are 2 answers

2
David vanbelle On

it is resolved

i add all verbs in IIS HTTP Handler by the web.config

   <system.webServer>
<handlers>
 <add name="OPTIONSVerbHandler" path="*" verb="*"    modules="ProtocolSupportModule" resourceType="Unspecified" requireAccess="None"  />
</handlers>
</system.webServer>
0
EyelineImagery On

You always have to Remove the Handler before Modifying it.

<handlers>
<remove name="OPTIONSVerbHandler" />
<add name="OPTIONSVerbHandler" path="*" verb="GET,HEAD,POST,OPTIONS" modules="ProtocolSupportModule" resourceType="Unspecified" requireAccess="None" />
</handlers>