默认情况下启用的Web API的所有HTTP动词? [英] Enabling all http verbs for Web API by default?

查看:267
本文介绍了默认情况下启用的Web API的所有HTTP动词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有为什么每个Web API的行动需要与例如装饰有原因的。

Is there a reason why each Web API action needs to be decorated with e.g.

[AcceptVerbs("Get", "Post")]

要接受专门Get和Post吗?有没有办法来全局允许所有动词,用于所有的行动,而不是依靠方法名Get或Post prefeix呢?

to specifically accept Get and Post? Is there a way to globally allow all verbs for all actions and not relying on Get or Post Prefeix of the method name instead?

多达有做事的休息方式,结合动词来操作,为什么不让在需要的时候都只是限制他们?

As much as there is the rest way of doing things, to bound verbs to Actions, why not allow all just restrict them when needed?

推荐答案

原因不能够在默认情况下是,它会破坏你的RESTful API。

The reason for not enabling that by default is that it would break your RESTful api.

如果您通过启用默认情况下所有的动词那么这将是无法确定要使用哪个动作。

If you enabled all verbs by default then it would be impossible to identify which action to be used.

让我们举个例子,你有一个 UsersController ,其中包含4种方法:获取所有用户获取特定用户帖子用户删除用户。这些方法只会造成两种不同的端点,并为发动机的WebAPI要能够选择它需要的HTTP动词的正确方法。

Let's take an example, you have a UsersController which contains 4 methods: Get all users, Get specific user, Post user and Delete user. These methods will only result in two different endpoints, and for the WebApi engine to be able to select the correct method it needs the HTTP verb.

UsersController

[RoutePrefix("api/users")]
public class UsersApiController : ApiController 
{
        [Route("")]
        public IHttpActionResult Get()
        {
            var result = _userRepository.GetAll();

            return Ok(result);
        } 

        [Route("{id:guid}")]
        public IHttpActionResult Get(Guid id)
        {
            var result = _userRepository.GetById(id);

            if (result == null)
                return NotFound();

            return Ok(result);
        }        

        [Route("")]
        public IHttpActionResult Post([FromBody]UserPostModel model)
        {
            var user = new User(model.FirstName, model.LastName);
            _userRepository.Add(user);           

            return Created<User>(Request.RequestUri + user.Id.ToString(), user);
        }

        [Route("{id:guid}")]
        public IHttpActionResult Delete(Guid id)
        {
            var result = _userRepository.Delete(id);

            if (!result)
                return BadRequest();

            return Ok();
        }         

        public class UserPostModel 
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }

        public class User
        {
            public User(string firstName, string LastName)
            {
                Id = Guid.NewGuid();
                FirstName = firstName;
                LastName = lastName;
            }

            public Guid Id { get; private set; }
            public string FirstName { get; private set; }
            public string LastName { get; private set; }        
        }
}

这篇关于默认情况下启用的Web API的所有HTTP动词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆