0

Im tring to set the values of the HttpWebResponse that is returned by the Controller but I can't create an HttpWebResponse object.

Right now I just want to set the status code but maybe I'll need other parts of the HttpWebResponseclass soon too. All I can do by now is returning strings as a status what is obviously wrong since Get methods already return other Data.

This is my controller(just a test application to understand how a REST server&client are implemented and how they communicate):

public class TeamsController : ApiController
{
    public static List<Team> teams = new List<Team>() 
    {
        new Team  { Kuerzel = "BVB09",Name="Borrusia Dortmund", Stadt="Dortmund"},
        new Team  { Kuerzel = "RWE",Name="Rot Weiss Essen", Stadt="Essen"}
    };

    public IEnumerable<Team> GetAllTeams()
    {
        Console.WriteLine("All teams returned");
        return teams;
    }

    public Team GetTeamById(int id)
    {
        if (id < teams.Count)
        {
            Console.WriteLine("Team with ID=" + id + " returned");
            return teams[id];
        }
        else
            return null;
    }

    public string PostNewTeam(Team team)
    {
        teams.Add(team);
        Console.WriteLine("Post Team: " + team.ToString());
        return "Success";
    }

}
Marv
  • 119
  • 3
  • 14

2 Answers2

1

use using System.Net.Http; and add return Request.CreateResponse(HttpStatusCode.OK, "success"); during PostNewItem() method return; Return type of the Post() method should be HttpResponseMessage

rt2800
  • 3,045
  • 2
  • 19
  • 26
  • Thanks! Thats what I was searching for. Did i understood correctly that the second parameter (T) is the Body of the Http Message? – Marv Apr 20 '16 at 06:25
0

You can try this:

public IHttpActionResult PostNewTeam(Team team)
{
    teams.Add(team);
    // Console.WriteLine("Post Team: " + team.ToString());
    return this.StatusCode(HttpStatusCode.Created);
}
mihkov
  • 1,171
  • 13
  • 37
  • Maybe I should have make it a little bit more visible: I can only use .net 4.0 therefore I have no access to `IHttpActionResult` – Marv Apr 19 '16 at 12:39