0

I new in .net web developpment and I'am working on a rest API with ASP.net,the problem is that I want to get the student by name that I put it in the URL,this is my model class Student.cs:

 public partial class Student : BaseEntity
        {

            public string FirstName { get; set; }
            public string LastName { get; set; }

//constructor of the Student
 public Student()
        {
            Attendances = new List<Attendance>();
            StudentComments = new List<StudentComment>();
        }
 }

this is my code of dataBase data initialisation:SMADBInitializer.cs

var Students = new List<Student>  //I get the error in this line
            {
            new Student{FirstName="sam", LastName="9"},
            }

the SMAContext.cs:

public partial class SMAContext : DataContext
    {
        static SMAContext()
        {
            Database.SetInitializer<SMAContext>(new SMADBInitializer());
        }

        public SMAContext()
            : base("Name=SMAContext")
        {
        }        
public DbSet<Student> Students { get; set; }
}

the StudentsController.cs:

private static SMAContext cc = new SMAContext();
    [Route("api/Students/{name:alpha}")]
            public string Get(string name)
            {
                var query = from Students in cc.Students
                            where Students.FirstName.Contains(name)
                            select Students.FirstName;

                var students = query.ToList();
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, students);
                return response.ToString();
            }

This is the RouteConfig class:

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
            routes.MapRoute(
               name: "GetStudentName",
               url: "{controller}/{action}/{name}",
               defaults: new { controller = "Home", action = "Index", name = UrlParameter.Optional }
           );
        }
    }

from a tutorial,I have commented those lines of the WebApiConfig file:

config.MapHttpAttributeRoutes();

            //config.Routes.MapHttpRoute(
            //    name: "DefaultApi",
            //    routeTemplate: "api/{controller}/{id}",
            //    defaults: new { id = RouteParameter.Optional }
            //);
            var json = config.Formatters.JsonFormatter;
            json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
            config.Formatters.Remove(config.Formatters.XmlFormatter);

I have created a html file in which I put all URLs of the application and this is for the select by name for the student:

<h4>Students</h4>
<a href="api/Students/sam" target="_blank">(GET => api/Students/sam) GetByName &raquo;</a><br />

The problem is that when I try to tape http://localhost:50001/api/Students/sam in the navigator,I get this error:

"ExceptionType":"System.NullReferenceException"

enter image description here

so I get the query variable null in StudentsController

thanks for help :)

hanali
  • 227
  • 7
  • 17
  • Which line of code causes the NullReferenceException? – mason Oct 01 '15 at 15:40
  • @mason I get the error in this line of code: var Students = new List //I get the error in this line { new Student{FirstName="sam", LastName="9"}, } – hanali Oct 01 '15 at 15:42
  • no it's no mine Craig W. :) – hanali Oct 01 '15 at 15:43
  • Does `Student` or `BaseEntity` have a parameterless constructor? – mason Oct 01 '15 at 15:44
  • no @mason this is te constructor of Student.cs – hanali Oct 01 '15 at 15:45
  • I have updated my question :) – hanali Oct 01 '15 at 15:47
  • You need to identify what exactly is null. We can't really help you until you do that. – mason Oct 01 '15 at 15:52
  • I think that the problem is on routing,when I try to put this line in navigator http://localhost:50001/api/Students/sam I get an error from the file SMADBInitializer.cs at the line var Students = new List //I get System.NullReferenceException in this line { new Student{FirstName="sam", LastName="9"}, } – hanali Oct 01 '15 at 15:57

0 Answers0