使用Http动词属性的WebApi 2属性路由 [英] WebApi 2 Attribute Routing using Http Verb Attributes

查看:207
本文介绍了使用Http动词属性的WebApi 2属性路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

动手使用ASP.NET

While having Hands on for the ASP.NET Web API2 Attribute Routing feature, I observe some strange differences for this feature.

  1. 要使用属性路由,我们必须使用Route(")属性并指定自定义路由.

  1. To use attribute route we have to use the Route("") attribute and specify the custom routes.

[Route("api/books")]
public IEnumerable<Book> GetBooks() { ... }

[Route("api/books/{id:int}")]
public Book GetBook(int id) { ... }

  • WebAPI 2中的 HttpPost HttpGet HttpPut HttpDelete 属性也有所变化.它引入了附加的构造函数,以使用Attribute指定路由.(示例- Dino的简单谈话 Visual Studio杂志文章)

  • Also there is change in HttpPost, HttpGet, HttpPut and HttpDelete attributes in WebAPI 2. It introduces additional constructor to specify the Routing using the Attribute. (Example - Simple Talk by Dino and Visual Studio Magazine article)

    [HttpGet("orders/{id:int:range(1, 100)}/show"]
    

  • 我为Visual Studio 2012安装了Microsoft ASP.NET和Web工具2013.1,并使用Web API2空项目模板创建了示例WebAPI2应用程序.如果将HttpPost属性与route作为字符串一起使用,则会产生错误,构造函数无法获取属性.

    I installed the Microsoft ASP.NET and Web Tools 2013.1 for Visual Studio 2012 and using the the Web API2 Empty Project Template created the Sample WebAPI2 application. There If we use the HttpPost Attribute with route as string it gives error, Constructor cannot take attribute.

    我还使用nuget包Microsoft.AspNet.WebApi版本5.0.0尝试了相同的操作.然后,我下载了 Visual Studio杂志文章.但是奇怪的是,我可以看到使用HttpPost属性的情况,该属性需要输入路由.我注意到有一个区别,即 System.Web.Http.HttpPostAttribute 是从 HttpVerbAttribute 派生的,就像我在 System.Web.Http.HttpPostAttribute 源自 Attribute ,并实现 IActionHttpMethodProvider 接口

    I also tried the same thing using nuget Package Microsoft.AspNet.WebApi version 5.0.0. Then I downloaded the source code of Article listed in Visual Studio Magazine Article. But strange thing is there I can see the use of HttpPost attribute which takes input route. There is one difference i noticed that System.Web.Http.HttpPostAttribute is derived from HttpVerbAttribute, where as in my version of System.Web.Http.HttpPostAttribute is derived from Attribute and implements IActionHttpMethodProvider Interface

    我继续从 CodePlex 下载了Asp.NET Mvc源代码,但是奇怪的是,我没有找到具有默认构造函数和单参数构造函数的 System.Web.Http.HttpPostAttribute 实现.

    I went on and downloaded the Asp.NET Mvc source code from CodePlex, but strange I did not find the System.Web.Http.HttpPostAttribute implementation with default constructor and single parameter constructor.

    我在这里错过了什么吗?

    Am I missing anything here.

    推荐答案

    +1关于Darrel提到的内容.

    +1 on what Darrel mentioned.

    此外,帖子中 2.点中的格式也是 5.0 版本的最终版本之前支持的格式.因此,请确保您的MVC和Web API nuget程序包具有以下版本,以支持 1 中的格式.它们应如下所示:

    Also the format in 2. point in your post is a format which was supported before the final release of version 5.0. So make sure that your MVC and Web API nuget packages are of following version to support the format in 1. They should look something like below:

      <package id="Microsoft.AspNet.Mvc" version="5.0.0" targetFramework="net451" />
      <package id="Microsoft.AspNet.Razor" version="3.0.0" targetFramework="net451" />
      <package id="Microsoft.AspNet.Web.Optimization" version="1.1.1" targetFramework="net451" />
      <package id="Microsoft.AspNet.WebApi" version="5.0.0" targetFramework="net451" />
      <package id="Microsoft.AspNet.WebApi.Client" version="5.0.0" targetFramework="net451" />
      <package id="Microsoft.AspNet.WebApi.Core" version="5.0.0" targetFramework="net451" />
      <package id="Microsoft.AspNet.WebApi.HelpPage" version="5.0.0" targetFramework="net451" />
      <package id="Microsoft.AspNet.WebApi.WebHost" version="5.0.0" targetFramework="net451" />
    

    这篇关于使用Http动词属性的WebApi 2属性路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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