2

I am reading Designing Evolvable Web APIs with ASP.NET. In one of the exercises, the book has me edit a Controller using Visual Studio. This is being done in ASP.NET using C#. The template I used was the standard ASP.NET web application API.

I have edited the controller to the way the book shows (although it does not seem to give very specific directions). Here is what my controller looks like.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OAuth;
using WebApplication4.Models;
using WebApplication4.Providers;
using WebApplication4.Results;

namespace WebApplication4.Controllers
{
    public class GreetingController : ApiController
    {
           public string GetGreeting() {
            return "Hello World!";
            }

    }
    public static List<Greeting> _greetings = new List<Greeting>();
    public HttpResponseMessage PostGreeting(Greeting greeting)
    {
        _greetings.Add(greeting);
        var greetingLocation = new Uri(this.Request.RequestUri, "greeting/" + greeting.Name);
        var response = this.Request.CreateResponse(HttpStatusCodeResult.Created);
        response.Headers.Location = greetingLocation;
        return response;

    }
}

I get errors on:

  • _greetings: A namespace cannot directly contain members such as fields or methods
  • PostGreeting: A namespace cannot directly contain members such as fields or methods,
  • _greetings : does not exist in the current context
  • Request : <invalid-global-code> does not contain a definition for 'request',
  • Created: HttpStatusCodeREsult does not contain a definition for 'Created'
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
normandantzig
  • 169
  • 2
  • 8
  • Note that your code is not thread-safe and will not work with concurrent requests. – SLaks Aug 21 '15 at 01:44
  • Side note: it is generally good idea to search for error message as someone else before you could have hit similar issue. Generally posts with misplaced braces/commas are closed as "typographical error", but if you don't like that it can be closed as duplicate of one of many "namespace cannot directly contain... " posts like http://stackoverflow.com/questions/9383791/a-namespace-cannot-directly-contain-members-such-as-fields-or-methods. – Alexei Levenkov Aug 21 '15 at 02:20
  • @AlexeiLevenkov I don't see this as typographical error as I am not clear on why the brace needs to be moved. From an answer below, I see that the _greetings should be part of the greetingController class, but I do not understand the relationship that is occuring between the class and the list (or code after it). – normandantzig Aug 23 '15 at 02:53

2 Answers2

14

As the error is trying to tell you, your fields and methods must be inside the class.
Check your braces.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • +1 accidentally deleted a curly brace and spent some time before I figured out what was the actual problem. – Sharif Mar 01 '17 at 22:47
2

Your _greetings field needs to be part of the class, as well as the PostGreeting method, it seems you just closed "}" of the class a bit early. MOve the "}" before the _greetings field to the end of the file, like:

namespace WebApplication4.Controllers
{
    public class GreetingController : ApiController
    {
           public string GetGreeting() {
            return "Hello World!";
            }

    public static List<Greeting> _greetings = new List<Greeting>();
    public HttpResponseMessage PostGreeting(Greeting greeting)
    {
        _greetings.Add(greeting);
        var greetingLocation = new Uri(this.Request.RequestUri, "greeting/" + greeting.Name);
        var response = this.Request.CreateResponse(HttpStatusCodeResult.Created);
        response.Headers.Location = greetingLocation;
        return response;

    }
}  
}
Carlos Aguilar Mares
  • 13,411
  • 2
  • 39
  • 36
  • Agular Mares After making the suggested change, all but one error cleared up. I now only have an error with Created: HttpStatusCodeREsult does not contain a definition for 'Created'. On a side note, why does my _greetings field need to be part of the class? – normandantzig Aug 23 '15 at 02:46
  • I think that code is incorrect, my guess is instead you meant: HttpStatusCode.Created. In terms of why _greetings needs to be part, well basically namespaces cannot have any state/data stored in them, they are really just "aliases/suffix-names/container-ishs" for types, so you cannot have state/variables/fields at that level. – Carlos Aguilar Mares Aug 24 '15 at 18:25