Metaphor
Metaphor

Reputation: 6405

How do I form the URL to call this WebAPI function?

I have this controller in an MVC 5 WebAPI application and I can't seem to figure out how to form the URL to call it. I keep getting a 404. tried .../ssa, /ssa/ssamedians, /ssa/ssamedians?titles=abc... What am I missing?

public class ssaController : ApiController
{
    public IHttpActionResult getSsaMedians(string Titles = "")
    {
        SsaDB db = new SsaDB();
        try
        {
            IEnumerable<Title_Medians> medians = db.getTitleMedians(Titles, null, null);
            System.Web.HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "*");
            return Ok(medians);
        }
        catch
        {
            return NotFound();
        }
    }

There are also Actions called getSsaMediansByAaa() and getSsaMediansByBbb(). Once I got rid of the api/ in the routeTemplate, I now get a "Multiple actions were found that match the request".

Upvotes: 1

Views: 55

Answers (2)

nik0lai
nik0lai

Reputation: 2655

Open your WebApiConfig.cs file and add this:

config.Routes.MapHttpRoute(
    name: "SSATitles",
    routeTemplate: "api/{controller}/{action}/{Titles}",
    defaults: new { controller = "ssa", action = "getSsaMedians", Titles = UrlParameter.Optional } 
);

For more information on webapi routing look here:

http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

Upvotes: 1

kevinkrs
kevinkrs

Reputation: 108

You have an parameter in your method, that means that you need to add an extra link in your url.

Examlple.com/DirToApi/getSsaMedians/YourString

You may also want to check your RouteConfig file in your App_Start

Upvotes: 1

Related Questions