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'
3
  • Note that your code is not thread-safe and will not work with concurrent requests. Commented Aug 21, 2015 at 1: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 stackoverflow.com/questions/9383791/…. Commented Aug 21, 2015 at 2: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). Commented Aug 23, 2015 at 2:53

2 Answers 2

14

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

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

1 Comment

+1 accidentally deleted a curly brace and spent some time before I figured out what was the actual problem.
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;

    }
}  
}

2 Comments

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?
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.

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.