【问题标题】:ASP.Net WebApi change this default request method mapping with action nameASP.Net WebApi 使用操作名称更改此默认请求方法映射
【发布时间】:2017-11-07 13:08:55
【问题描述】:

我正在创建一个名为 Customer 的新 Web API 控制器。 这个控制器有一个名为“创建”的动作

我无法通过“GET”HTTP 请求来请求此操作 以这种形式

http://ip:port/api/Customer/Create?userId=x&password=y

这种方法除外:

public class CustomerController : ApiController
{
    [System.Web.Mvc.AcceptVerbs("GET", "POST")]
    [System.Web.Mvc.HttpGet]
    [ActionName("Create")]
    public MISApiResult<List<Branch>> GetCreate(string userID, string password)
    {
        return new MISApiResult<List<Branch>>() { Result = new List<Branch>() { new Branch() { ID = Guid.NewGuid(), Name = "Branch1" } }, ResultCode = 1, ResultMessage = "Sucess" };
    }
}

是否有任何其他解决方案可以将操作名称保留为“创建”作为下一个。

public class CustomerController : ApiController
{
    [System.Web.Mvc.AcceptVerbs("GET", "POST")]
    [System.Web.Mvc.HttpGet]
    public MISApiResult<List<Branch>> Create(string userID, string password)
    {
        return new MISApiResult<List<Branch>>() { Result = new List<Branch>() { new Branch() { ID = Guid.NewGuid(), Name = "Branch1" } }, ResultCode = 1, ResultMessage = "Sucess" };
    }
}

谢谢。

编辑:

很抱歉第一次没有说清楚。

根据这个答案:

How does a method in MVC WebApi map to an http verb?

根据动作名称有一个默认的http方法,如果以Get开头,则默认映射为GET HTTP方法,否则将映射为POST。

有没有一种方法可以使用自定义映射更改此默认映射,以便我可以使用“GET”Http 方法映射名为“Create”的操作以用于测试目的,因为这种方式对开发来说更快

我尝试将 HttpGet Attribute 和 AcceptVerbs("GET") 放入,它仍然使用 POST Http 方法映射操作。

我找到了一种方法,就像我说的那样,将操作方法​​名称更改为 GetCreate,然后将 ActionName 属性与“Create”值一起放入。 但是有没有办法改变默认映射?

再次感谢。

【问题讨论】:

  • 您正在创建一个客户。如何获取请求。它需要是一个发布请求
  • 你能告诉我们为什么你需要 Create as action Name name 和 Get as HttpMethod.?你有理由吗?
  • 在开发阶段测试Action URL更快。稍后我会在它发布时将其改回 POST。
  • 所以。它是关于正确的操作 URL,这就是为什么我们有控制器的 [RoutePrefix("Customer")] 和 [Route("Create")]
  • 另外,为什么你必须同时接受 GET 和 post 以相同的操作方法。我认为这根本不是一个好习惯。对于分别获取和发布的两个操作方法,您可以拥有相同的 URL。尝试在开发阶段做最佳实践。您的个人资料似乎有 8 年的经验。我无法从你的问题中找到。

标签: c# asp.net-web-api asp.net-web-api-routing


【解决方案1】:

您可以在此操作之前使用自定义路由:

[HttpGet]
[Route("customer/create")]
public MISApiResult<List<Branch>> Create(string userID, string password)

不要忘记在应用配置期间启用属性路由(这应该在默认路由定义之前添加):

config.MapHttpAttributeRoutes();

虽然我建议遵循约定并使用适当的 HTTP 动词 - 如果您正在创建客户,那么按照约定,您应该使用 POST 请求到端点 api/customers。否则你的 API 可能会让其他人感到困惑。

我还建议使用IHttpActionResult 作为方法的返回类型:

 public IHttpActionResult Post(string userID, string password)
 {
     if (_userRepository.Exists(userID))
         return BadRequest($"User with ID {userID} already exists");

     _userRepository.Create(userID, password);
     return StatusCode(HttpStatusCode.Created) // or CreatedAtRoute
 }

延伸阅读:Attribute Routing in ASP.NET Web API 2

【讨论】:

    【解决方案2】:

    你为什么不指定路线。你的实际问题是使用System.Web.Mvc 改用System.Web.Http

     using System.Web.Http;
     [RoutePrefix("api/Customer")]
        public class CustomerController : ApiController
        {
            [HttpGet]
            [Route("Create")]
            public MISApiResult<List<Branch>> Create(string userID, string password)
            {
                return new MISApiResult<List<Branch>>() { Result = new List<Branch>() { new Branch() { ID = Guid.NewGuid(), Name = "Branch1" } }, ResultCode = 1, ResultMessage = "Sucess" };
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      相关资源
      最近更新 更多