ayasha
ayasha

Reputation: 1251

REST API Routing

I would like to use the following in my Controller:

[Route("api/{controller}/{action}")]

instead of using RouteTable.Routes.MapHttpRoute(...). The problem is that it says that Route is not an attribute class. I have included the following libraries:

using System.Web.Http;
using System.Web.Http.Description;
using System.Web.Http.Controllers;
using System.Web.Routing;
using AttributeRouting.Web.Http;
using System.Web.Mvc;

I have Microsoft.Aspnet.Mvc 5.2.3 and Web API 2.2. I tried to follow this http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx and added

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
       routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
       routes.MapMvcAttributeRoutes();
    }
}

but it says that IgnoreRoute and MapMvcAttributeRoutes don't exists.. What am I doing wrong? Thanks

Upvotes: 1

Views: 1595

Answers (2)

matt_lethargic
matt_lethargic

Reputation: 2786

I've just created a quick test project using Mvc 5.2.2. In your controller you only need

 using System.Web.Mvc;

For the RouteAttribute and in the RouteConfig only

using System.Web.Mvc;
using System.Web.Routing;

Having using System.Web.Http; and using AttributeRouting.Web.Http; is prob just confusing the situation (no idea what library AttributeRouting.Web.Http is from or is this a typo?). Your WebApi config should be done in a different class. Also make sure you have System.Web.Mvc.dll referenced, just in case

Upvotes: 0

Leonardo Festa
Leonardo Festa

Reputation: 101

In web api you can use the attribute RoutePrefix that works as a prefix for all the controllers actions

You can do something like:

[RoutePrefix("api/apps")]
public class ApplicationsController : ApiController{

        [Route("get/{id}"))]
        public async Task<IHttpActionResult>GetApplications(int id)

Upvotes: 1

Related Questions