Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RoutePrefixAttribute in ASP.NET 5

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?

like image 638
alexxjk Avatar asked Aug 01 '15 21:08

alexxjk


People also ask

What is RoutePrefix MVC?

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.

How do I enable attribute routing?

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.


1 Answers

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.

like image 102
Henk Mollema Avatar answered Oct 29 '22 02:10

Henk Mollema