如何在http请求中允许Post方法?

时间:2015-06-16 10:17:09

标签: asp.net wcf iis post iis-8.5

我正在尝试使用标准HTTP谓词(GET,POST,PUT,DELETE)创建RESTFul服务。我使用RESTful注释的经典WCF服务。我的问题是我得到了一个不允许的方法"用POST请求我的服务时。

注意:我使用的是IIS 8.5 asp.net

这是我的代码:

的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);


}

}

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


        }
        }

通常情况下,在我的web.config中使用此自定义标头我希望有权发送帖子ajax $

实际上我已经请求获取它的好但不适合请求POST

当服务器发送此错误消息时,我不太了解 它有可能帮助我吗?谢谢

1 个答案:

答案 0 :(得分:1)

已解决

我通过web.config

在IIS HTTP Handler中添加所有动词
   <system.webServer>
<handlers>
 <add name="OPTIONSVerbHandler" path="*" verb="*"    modules="ProtocolSupportModule" resourceType="Unspecified" requireAccess="None"  />
</handlers>
</system.webServer>