3

I am coding a C# web api 2 webservice and would like some help to get a single item from a request to a webservice.

Here is the web service controller class code:

[RoutePrefix("api")]
public class ItemsWebApiController : ApiController

Here is the web service function:

// GET: api/Getitem/1
[Route("Getitem")]
[System.Web.Http.HttpGet]
[ResponseType(typeof(Item))]
public async Task<IHttpActionResult> GetItem(int id)
{
    Item item = await db.items.FindAsync(id);
    if (item == null)
    {
        return NotFound();
    }

    return Ok(item);
}

Here is the uri to the IIS website:

http://localhost/thephase

Here is the uri that I am accessing:

http://localhost/thephase/api/Getitem/1

Here is the error that is being displayed in the browser:

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost/thephase/api/GetItem/1'.","MessageDetail":"No type was found that matches the controller named 'GetItem'."}

Here is the WebApiConfig code:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
        var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
        config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
    }
}

The error states that the controller is named 'GetItem', and this is incorrect. Hence I am thinking that the problem is in the WebApiConfig route code.

If I remove the int id from the function, then the function is called correctly.

Here is the same function with no parameter:

// GET: api/Getitemnoparameter
[Route("Getitemnoparameter")]
[System.Web.Http.HttpGet]
[ResponseType(typeof(Item))]
public async Task<IHttpActionResult> GetItem()
{
    Item item = await db.items.FindAsync(1);
    if (item == null)
    {
        return NotFound();
    }

    return Ok(item);
}

The following uri accesses the function correctly:

http://localhost/thephase/api/Getitemnoparameter

So the problem has something to do with the int parameter.

Can someone please help me to get access the GetItem function with a parameter?

2
  • Is it because the route is case sensitive (Getitem versus GetItem)? Commented Jan 26, 2016 at 2:18
  • I have edited the uri to be Getitem. Commented Jan 26, 2016 at 2:24

2 Answers 2

3

Because you are using Attribute Routing you also need to specify the parameter for it to work.

Check this tutorial out for a better understanding.

Route Prefixes

[Route("Getitem/{id:int}")]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. The code is now being accessed correctly.
1

The Id is an int. int is a value type. Value types cannot be null. You set the parameter to RouteParameter.Optional, but if it's optional, it must be able to be assigned null.

Solution: Use a nullable int

public async Task<IHttpActionResult> GetItem(int? id)

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.