0
[RoutePrefix("web/api")]
public class UsersController : BaseController
{
    [Route("users")]
    [HttpGet]
    public Response<List<UserDTO>> Get(string q, string ex)

I am trying to make the get function respond to : web/api/users?q=sd&ex=1

But it is not working?

Rahul Kushwaha
  • 421
  • 7
  • 14

5 Answers5

1

I gess your mistake is that you using RoutePrefix on controller class with Route on method.

If you want to call url like you show you should define your controller like this:

[RoutePrefix("web/api/users")]
public class UsersController : BaseController
{
    [HttpGet]
    public Response<List<UserDTO>> Get(string q, string ex)
teo van kot
  • 12,350
  • 10
  • 38
  • 70
0

Try This :

`[RoutePrefix("web/api")]
public class UsersController : BaseController
{
    [Route("users/{q}/{ex}")]
    [HttpGet]
    public Response<List<UserDTO>> Get(string q, string ex)`

and just call api like : web/api/users/text1/text2

0

You can add custom routes

config.Routes.MapHttpRoute(
    name: "NameOfRoute",
    routeTemplate: "web/{controller}/{action}?q={q}&ex={ex}",
    defaults: new { controller = "controller-name", action = "action-name", 
        q = RouteParameter.Optional, ex= RouteParameter.Optional }
    );
Sandip Bantawa
  • 2,822
  • 4
  • 31
  • 47
0

First of all, in order to be able to have multiple GET methods in the same controller, you must add your custom routes:

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

Then, in your controlller you need to mark the parameter as FromUri in order to tell the method to expect the parameter from the URI. This way, you will be able to call the method the following way:

/web/api/users/Get?q="your string1"&ex="ex"

public Response<List<UserDTO>> Get([FromUri]string q, [FromUri]string ex)

If this is the only GET method, you may skip the part with mapping custom routes.

Hope this helps.

Best of luck!

radu-matei
  • 3,469
  • 1
  • 29
  • 47
0
[RoutePrefix("web/api")]
public class UsersController : BaseController
{
    [Route("users")]
    [HttpGet]
    public Response<List<UserDTO>> Get(string q, string ex)

Sorry to bother you all, but this works perfectly fine. What I was doing wrong is that I had some controllers in the MVC project which pointed to the same url. As a result I was getting 404.

But thanks a lot all.

Rahul Kushwaha
  • 421
  • 7
  • 14