1

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

  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) { ... }
    
  2. 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"]
    

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.

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

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.

2 Answers 2

3

Web API and MVC are two completely different frameworks that have classes with overlapping functionality. Don't ever reference MVC namespaces in Web API related classes. In fact, ideally, I would create a separate project to hold your web API related stuff and don't have a reference to System.Web. That should keep you safe from collisions.

Sign up to request clarification or add additional context in comments.

Comments

1

+1 on what Darrel mentioned.

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" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.