EvilDr
EvilDr

Reputation: 9610

Cannot define `Name` attribute on WebAPI2 method

In Mike Wasson's WebAPI2 tutorials, the Name attribute of a route is clearly defined like this:

public class BooksController : ApiController
{
    [Route("api/books/{id}", Name="GetBookById")]
    public BookDto GetBook(int id) 
    {
        // Implementation not shown...
    }
}

However, when I try this in my own code (albeit VB) I can an error:

Type 'Name' is not defined

Is anything wrong with my approach?

VS error details

Upvotes: 0

Views: 35

Answers (2)

CodeCaster
CodeCaster

Reputation: 151720

The syntax you use will allow for multiple attributes. This causes the compiler think you want to use a NameAttribute, which doesn't exist.

Remove the closing parenthesis after the route string and use the "pascal assignment operator" :=:

<Route("api/books/{id}", Name:="GetBookById")>

As shown on Attributes (C# and Visual Basic).

Upvotes: 2

matt_lethargic
matt_lethargic

Reputation: 2796

Intellisense to the rescue!! Don't use the Name property name:

<Route("test/{cid}", "nameofroute", 0)>
Public Function [Get](cid As Int32)

End Function

Upvotes: 1

Related Questions