0

I am trying to set up an Asp.Net forms site with an API. I have succeeded in adding in selective authentication, so that pages starting "\api\" do not get redirected, but instead challenge for basic authentication. I am now trying to use MS Web Api 2 to do the API routing.

The idea is to be as RESTful as possible. I have a resource, a TradableItem, and initially I would want to allow API users to use HTTP GET in one of two ways.

If the API user passes no item key, the user receives a list of possible item keys

  ["ABC","DEF"...]

If the API user passes an item key as part of the URI, eg "/api/tradables/abc", a representation of the TradableItem is returned for the one with the key=ABC. (To my understanding, this is standard REST behaviour).

In Global.ASAX's Application_Start() function I have a route map like so...

    RouteTable.Routes.MapHttpRoute(
        name: "TradableItemVerbs",
        routeTemplate: "api/tradables/{item}",
        defaults: new { item = System.Web.Http.RouteParameter.Optional, controller = "Tradable" });

The TradableController.cs file looks like this...

public class TradableController : ApiController
{
    private static CustomLog logger = new CustomLog("TradableController");

    // GET api/<controller>
    public IEnumerable<string> GetKeys()
    {
    var prefix = "GetKeys() - ";
    string msg = "";

    msg = "Function called, returning list of tradable pkeys...";
    logger.Debug(prefix + msg);

    // Get a list of tradable items
    return TradableManager.GetTradablePkeys();
    }

    // GET api/<controller>/<pkey>
    public string GetTradable(string pkey)
    {
    string msg = string.Format("Would get Tradable data for key: >{0}<", pkey);
    return msg;
    }
}

The problem is that only the GetKeys() function fires, whether I call GET to "/api/tradables" or "/api/tradables/abc".

For reference, using VS2015 Community, IIS 7.5, targeting .Net 4.6.1. I used Rick Strahl's blog as a guide on this (among other sources).

http://weblog.west-wind.com/posts/2012/Aug/21/An-Introduction-to-ASPNET-Web-API#HTTPVerbRouting

1 Answer 1

1

please, change the name of your param to item (because, this is the name define in the routes):

public string GetTradable(string item)
{
    ....
}

or when you call the method be explicit with the parameter name: /api/tradables?pkey=abc

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @julito-avellaneda, this worked. I was assuming the function would use the type, not the explicit name for the variable. Do you have any good links for WebAPI2 beginner tutorials (preferrably non MVC, but whatever).
Hi @SteveHibbert you can start with asp.net/web-api and next read the apress books: apress.com/catalogsearch/result/?q=web+api&submit=Go

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.