here is how I have routing setup.

 routes.MapHttpRoute(
                        name: "Authors",
                        routeTemplate: "api/authors",
                        defaults: new { controller = "authors" }
                    );

controller action method

// GET /api/authors/
    public string GetAuthors(string author_ids)
    {
        return data;
    }

Url http://site.com/api/authors?author_ids=1 actually calls controller action but when I don't pass querystring parameter, it says no controller action matching found.

How to handle optional querystring parameter when defining route?

share|improve this question
feedback

1 Answer

// GET /api/authors/
    public IEnumerable<string> GetAuthors()
    {
        return data;
    }

You will need to define an action that takes no parameters.

It would be better, however, to add id to your route as optional:

routes.MapHttpRoute(
                        name: "Authors",
                        routeTemplate: "api/{controller}/{id}",
                        defaults: new { id = RouteParameter.Optional }
                    );
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.