pixelmike
pixelmike

Reputation: 1983

Why was no action found for route?

My WebApiConfig.cs contains just this one route:

config.Routes.MapHttpRoute(
    name: "Calendar",
    routeTemplate: "api/Calendar/{date}",
    defaults: new { controller = "Calendar", action = "Get", date = RouteParameter.Optional }
);

And my CalendarController class has only this method:

public IEnumerable<FVEvent> Get( string dateStr )
{
    return db.Events.ToList();
}

But when I try hitting /api/Calendar in the browser, I get the following MessageDetail in the XML:

No action was found on the controller 'Calendar' that matches the request.

Plus I don't know why it's XML, I'd like it to be JSON.

There's just so much hidden magic going on I can't make a slight adjustment to a simple example without everything falling apart.

Upvotes: 0

Views: 45

Answers (2)

esmoore68
esmoore68

Reputation: 1296

Your route marks the dateStr parameter as optional, but the method signature does not. Try adding a default value to your dateStr parameter:

public IEnumerable<FVEvent> Get(string dateStr = "")
{
    return db.Events.ToList();
}

Upvotes: 1

su8898
su8898

Reputation: 1713

Make sure the name of your Calendar Controller ends with Controller. Also use the following code in your WebAPI config to remove the XML formatter.

var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

Upvotes: 0

Related Questions