DaveDev
DaveDev

Reputation: 42175

How can I fix my route configuration to accept a second parameter?

I've modified my Get() endpoint to take another parameter, and it doesn't work anymore

public async Task<AjaxResponse> Get(long id, string appType)
{

}

This is the route config that I've added for the new parameter (if I don't add this, the request returns a 404):

routes.MapHttpRoute(  
    name: "GetUser",
    routeTemplate: "api/{controller}/{userId}/{appType}"
    );

Now requests for that endpoint, e.g.

/api/AccountApi/343434338361295/customer

end up at the default framework generated Get() endpoint:

public IEnumerable<string> Get()
{
    return new string[] { "value1", "value2" };
}

What's wrong with my routing configuration, and what can I do to fix it?

For reference, my entire route configuration is as follows:

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

    //ASP.NET Web API Route Config
    routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
        );

    routes.MapHttpRoute(
        name: "CreateOrLogin",
        routeTemplate: "api/{controller}/CreateOrLogin/{appType}/{userId}"
        );

    routes.MapHttpRoute(
        name: "GetUser",
        routeTemplate: "api/{controller}/{userId}/{appType}"
        );

    routes.MapHttpRoute(
        name: "GetAdsForRetailer",
        routeTemplate: "api/{controller}/GetAdsForRetailer/{userId}/{page}/{pageSize}"
        );

    routes.MapHttpRoute(
        name: "GetLatestAds",
        routeTemplate: "api/{controller}/GetLatestAds/{userId}/{maxId}"//
        );

    routes.MapHttpRoute(
        name: "GetAd",
        routeTemplate: "api/{controller}/GetAd/{userId}/{id}"//
        );

    routes.MapHttpRoute(
        name: "RegisterUser",
        routeTemplate: "api/{controller}/RegisterUser/"//
        );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

Upvotes: 1

Views: 137

Answers (2)

Babu Swami
Babu Swami

Reputation: 798

There are two mistakes in your code:

Firstly, The name of your first parameter is not correct. Either use userId or update your route config as @paolo-tedesco said.

Apart from it, the types of parameters in Actions should always be nullable. You're supplying long for userId. Rest of your code is perfect. Just try using long? in the action.

public async Task<AjaxResponse> Get(long? userId, string appType)
{

}

Upvotes: 0

Paolo Tedesco
Paolo Tedesco

Reputation: 57172

I cannot test this right now, but I think that the names you define in the route must match those in the Get method, i.e. instead of

routeTemplate: "api/{controller}/{userId}/{appType}"

you should have

routeTemplate: "api/{controller}/{id}/{appType}"

The route should match regardless of the names you give, but then the framework will probably try to use reflection to determine the method to call, and for that parameter names will be relevant.

Upvotes: 2

Related Questions