Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Syntax and conventions

Tags:

c#

asp.net

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'
like image 323
normandantzig Avatar asked Dec 04 '25 18:12

normandantzig


1 Answers

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

like image 72
SLaks Avatar answered Dec 06 '25 08:12

SLaks