I've started a new Web API 2.0 project in ASP.NET 5. I try to create custom RoutePrefixAttribute class but I get this error
The type or namespace name 'RoutePrefixAttribute' could not be found
(are you missing a using directive or an assembly reference?) {ProjectName}.DNX Core 5.0
Should I use some other class instead?
RoutePrefix attribute is used to specify the common route prefix at the controller level to eliminate the need to repeat that common route prefix on each and every controller action method.
To enable Attribute Routing, we need to call the MapMvcAttributeRoutes method of the route collection class during configuration. We can also add a customized route within the same method. In this way we can combine Attribute Routing and convention-based routing. A route attribute is defined on top of an action method.
There is indeed no RoutePrefixAttribute
in MVC 6. Applying a [Route]
attribute on a controller will now act as a route prefix:
[Route("api/[controller]/[action]")]
public class ProductsController : Controller
{
[Route("{id:int}")]
public JsonResult Details(int id)
{
// ...
}
}
This will match api/Products/Details/42
.
Also see this blogpost by Filip W.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With