1

Currently i'm working on a web api using Microsoft MVC framework. Inside their docs i read the following (https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.1):

For convenience, attribute routes support token replacement by enclosing a token in square-braces ([, ]). The tokens [action], [area], and [controller] will be replaced with the values of the action name, area name, and controller name from the action where the route is defined. In this example the actions can match URL paths as described in the comments:

[Route("[controller]/[action]")] public class ProductsController : Controller { [HttpGet] // Matches '/Products/List' public IActionResult List() { // ... } [HttpGet("{id}")] // Matches '/Products/Edit/{id}' public IActionResult Edit(int id) { // ... } } 

Token replacement occurs as the last step of building the attribute routes. The above example will behave the same as the following code:

public class ProductsController : Controller { [HttpGet("[controller]/[action]")] // Matches '/Products/List' public IActionResult List() { // ... } [HttpGet("[controller]/[action]/{id}")] // Matches '/Products/Edit/{id}' public IActionResult Edit(int id) { // ... } } 

However, whenever i try to use the [HttpGet("my/route")] attribute, visual studio keeps telling me "HttpGetAttribute does not contain a constructor that takes 1 argument". I already read that i should install Microsoft.AspNet.WebApi.WebHost using the package manager, but the error is still there.

My question is, how can i start using the proper attribute? I'm not verry experienced with installing packages in Visual studio.

Thanks in adavance for any help.

3
  • What version is your Web api? Commented Oct 9, 2018 at 10:45
  • 1
    The code you have shown is for core-mvc, so I can only assume you are not using core-mvc Commented Oct 9, 2018 at 10:45
  • Also check what version of Web api is installed, attribute routing is supported starting from Web api 2. Commented Oct 9, 2018 at 10:46

3 Answers 3

9

Only ASP.NET Core has attribute routing with Http[Verb] attributes. If you have ASP.NET Framework project, you should use RouteAttribute.

ASP.Net Core: https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.1

ASP.NET Framework: https://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

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

Comments

3

try This

[RoutePrefix("Products")] public class ProductsController : Controller { [HttpGet] [Route("api/List")] public IActionResult List() { // ... } [HttpGet] [Route("api/Edit/{id}")] public IActionResult Edit(int id) { // ... } } 

3 Comments

You are not answering the question - just offering an alternative.
Thank you for your help. Can i use this in combination with the default routing? or is this only possible if i supply your proposed route attribute on every controller action? I ask this because the route now returns a 404..
try above my edited Code.i hope it's work well and i hope all dought will be cleared after referee code.Thanks
0

For anybody else googling this and going crazy like I was:

If you are using System.Web.Http for your class, you need to instead be using Microsoft.AspNetCore.Mvc. If you have both, it will be an ambiguous reference and give this error instead of telling you that (or at least that's what it did in my case)

Hope this helps someone save the trouble of reinstalling everything for no reason like I did!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.