10

I have a web api where I have 2 methods, one without parameter and two with different types of parameter (string and int). When calling the string method it doesnt work...what am I missing here?

public class MyControllerController : ApiController
{
    public IHttpActionResult GetInt(int id)
    {
        return Ok(1);
    }

    public IHttpActionResult GetString(string test)
    {
        return Ok("it worked");
    }
}

WebApiConfig.cs:

public static void Register(HttpConfiguration config)
{
    // Web API configuration and services

    // Web API routes
    config.MapHttpAttributeRoutes();

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

My Call:

/api/MyController/MyString //Doesnt work

/api/MyController/1 //work

I get following error:

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Http.IHttpActionResult GetInt(Int32)' in 'TestAngular.Controllers.MyControllerController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

What am I missing in my request?

6
  • 2
    You don't need the WebMethod attribute for WebAPI Commented Nov 19, 2015 at 8:42
  • 1
    You cannot use the same route for two different endpoint. The methode GetInt wins and will use for both requests. Commented Nov 19, 2015 at 10:07
  • How is the way to do it? add {action} in routeTemplate? Commented Nov 19, 2015 at 10:11
  • Set routeTemplate: "api/{controller}/{action}/{id}" and see my answer Commented Nov 19, 2015 at 10:25
  • Is that the only option I have ? Commented Nov 19, 2015 at 11:23

5 Answers 5

9

Here is my solution: without changing default route in webapiconfig.cs file

add just a route to your string function:

[Route("Api/MyController/GetString/{test}")]
public IHttpActionResult GetString(string test)

http://localhost:49609/api/MyController/GetString/stringtest

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

Comments

8

Also this uri's should work:

api/MyController/GetAll
api/MyController/GetString?param=string
api/MyController/GetInt?param=1

I think this is much clearer and should always work. You use the routing behavior.

See here: http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection

Comments

7

You have to change your uri's

/api/MyController

/api/MyController/string 

/api/MyController/1 

You don't have to specify the methods.

You could take look at this tutorial on asp.net for further clarification.

5 Comments

I tried to follow that tutorial and apply on my project but I didn't get to work..So thats why I'm asking here, I must have missed something
@emptyman so did you try to call these uri's and you didn't get any response?
I got the int to work, but how do I send a string parameter? It cant find the method (it looks for the int)
I don't think that you need any extra work. You simply have to make a get request to this /api/MyController/yourstringvalue
I've updated my question..Now the int method works fine (after tuning from your tutorial). But I can't get my string to work
4

It's been a while since you posted this, but I think I have the answer. First off, there are two issues. First, as Pinback noted, you can't use the same route for two different endpoints.

However if you just eliminate the int method, you'll still run into the problem. Remember: the default route looks like this: api/{controller}/{id} In order to bind the parameter, it has to be called "id", and not "test".

Change the signature to this:

public IHttpActionResult GetString(string id)

and it will work. (you can also change {id} to {test} in the webapiconfig.cs file).

Comments

0

You can also take string parameter in body

string body;
using (var sr = new StreamReader(Request.Body))
body = sr.ReadToEnd();

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.