Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problems Creating a simple WebAPI Service

Posted on 2014-11-24
5
Medium Priority
?
152 Views
Last Modified: 2014-11-25
I've been following these instructions on creating my first WebAPI service:

https://codecloudy.wordpress.com/2014/03/19/asp-net-web-api-101-basics/

I'll post the code below.

According to the article it should not only allow the browser to display results with such urls as:

http://localhost:56315/api/values
and
http://localhost:56315/api/values/1

but it should also be possible to send POST messages via Fiddler:

Post from Fiddler
However I am always getting a 500 error when I do this and no breakpoints get hit:

500 Error
I've tried publishing to my local IIS but I get the same problems - the browser displays ok but posting fails with a 500 error.

What am I doing wrong:

Here is the ValuesController.cs:

using System.Collections.Generic;
using System.Web.Http;

namespace FirstWebAPIService.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/values
        public void Post([FromBody]string value)
        {
        }

        public void Post(object value)
        {
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }
    }
}

Open in new window

0
Comment
Question by:purplesoup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40463036
If you change line 21 to:

public void Post([FromBody]string id)

Open in new window


...does it work? If it does, then I'll explain why.
0
 

Author Comment

by:purplesoup
ID: 40464319
Yes it does - very interesting... tell me more!!
0
 

Author Comment

by:purplesoup
ID: 40464331
Interesting - if I change this:

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

Open in new window


and this:

        // POST api/values
        public void Post([FromBody]string value)
        {
        }

Open in new window


it works again - although I had to change both {value} and value = RouteParameter.Optional - so I guess that is what I needed.

Also I found that Get(int id) stopped working through the browser - it was just returning the default IEnumerable, unless I changed the parameter to

        public string Get(int value)
        {
            return "value";
        }

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 40464640
The reason is that your default route expects a default parameter named id, but you created an action with a name of value. Model binding in MVC does an excellent job of putting values that the user sends into the appropriate variable or object. The only catch is that you have to set up the initial mapping. That's where the matching names come in. As you noticed, when you modified the default route to expect a variable named value, your action worked when the parameter's name was value.

Now, you can modify your default route, but if you do you'll have to ensure that any other action that would map to the default route maps in the expected way--i.e. however you set up the route. You can also add additional routes, but this can get both unwieldy and can cause issues if you don't list your routes in the correct order. Routing will take the first matching route, and routes are searched in the order they are added to the route table. You might consider using attribute routing. It's a bit more flexible as well as bit easier to understand than the default MVC routing structure.
0
 

Author Closing Comment

by:purplesoup
ID: 40464785
Thanks for your help - a great explanation too.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question