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

3zwtqj6y  于 2023-01-22  发布在  .NET
关注(0)|答案(2)|浏览(186)

我正在尝试使用标准HTTP predicate (GET、POST、PUT、DELETE)创建RESTFul服务。我使用带有RESTful注解的经典WCF服务。我的问题是在使用POST请求服务时得到"不允许的方法"。
注意:我使用的是IIS 8.5 www.example.comasp.net
下面是我的代码:

    • Web配置:**
<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>
    • 用户服务. 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);

}

}
    • 和用户服务实现服务:**
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与这个customheader我想有权限发送后ajax $
实际上,我有request GET,它很好,但不适合request POST
我不明白好当服务器发送此错误消息,有可能帮助我吗?谢谢

tp5buhyn

tp5buhyn1#

已经解决了
i通过web.config添加IIS HTTP处理程序中的所有 predicate

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

eaf3rand2#

在修改处理程序之前,必须始终删除它。

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

相关问题