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?

5 Answers 5

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)
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

it won't work with url like web/api/users?q=sd&ex=1
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 }
    );

1 Comment

I want to use Attribute Routing.
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!

1 Comment

I want to use attribute routing/
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.

Comments

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.