0

I have a method defined as follows

[Route("public/sites/GetAllForWeb")]
    [HttpGet]
    public IEnumerable<Domain.Site.Site> GetAllForWeb(DateTime date, int hour, string parameterCode = null)

That is then being called as follows (angular)

return this.http.get<Site[]>("public/sites/GetAllForWeb?date=" + date + "&hour=" + hour + ((parameterCode === undefined) ? "" : "&parameterCode=" + parameterCode)

Unfortunately the call doesn't match any controller methods, I am assuming because of the lack of parameters in the Route attribute.

How should the Route attribute be modified to correctly reflect the method signature?

7
  • The route attribute seems correct for this url. Do you have a routePrefix attribute on the controller ? Are the route attributes enabled ? ( do you have the line config.MapHttpAttributeRoutes(); in the webapi Config ?) Commented Jun 20, 2018 at 15:44
  • Have you tried ([FromURI] DateTime date, int hour) attribute for those two parameters and put /{date}/{hour} in Route attribute? Commented Jun 20, 2018 at 15:45
  • @J.Loscos No route prefix, and yes that line is in the config. Are the attributes not required to be in the route attribute for the framework to match the request with the method? Commented Jun 20, 2018 at 15:49
  • In your exemple all method arguments are http request parameters since they are after the "?" so you don't need to indicate them in the route attribute. Commented Jun 20, 2018 at 15:53
  • That's what I thought...and yet it doesn't work. Any other ideas? Commented Jun 20, 2018 at 15:55

1 Answer 1

1

This issues is that I didn't add api to the Route attribute :/

For some reason I was under the impression that api in

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

will get prepended to the Route attribute

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

1 Comment

This is the default route set up for convention routing which finds methods based on controller and action names. If you are using the Route attribute then you are attempting to use AttributeRouting which should be enabled by calling config.MapHttpAttributeRoutes(); If you do that you can delete the default convention route, and you will access your method with the address defined in your route attribute: public/sites/GetAllForWeb without the need for the /api bit.

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.