1

I want to use to asp.net web api different method in same api controller. I searched but I couldn't find.

For example:

public class HomeController : ApiController
{
    AdFindDBEntities db = new AdFindDBEntities();
    public HomeController()
    {
        db.Configuration.ProxyCreationEnabled = false;
    }

    [HttpGet]
    public List<Ad> AllAds()
    {
        return db.Ad.ToList();

    }

    [HttpGet]
    public IEnumerable<Ad> GetLastAds()
    {
        return db.Ad.OrderByDescending(x => x.CreatedDate).Take(20).ToList();
    }
}

When I run the project AllAds method running. I don't know how use to GetLastAds method. Please help me!

2 Answers 2

1

Use [Route] attribute for separation call you actions

[HttpGet]
[Route("api/home/ads}")]
public List<Ad> AllAds()
{
 ...
}


[HttpGet]
[Route("api/home/ads/last}")]
public List<Ad> GetLastAds()
{
 ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

change the default route to have {action} in the route.

routes.MapHttpRoute(
       name: "DefaultApi",
       routeTemplate: "api/{controller}/{action}/{id}",
       defaults: new { id = RouteParameter.Optional }

In that way you can call multiple actions by different urls.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.