简体   繁体   中英

I m getting a new error with this code, that didn't use to happen and I think I deleted something but I am not sure what

I was trying to find an issue and may have modified the code in an unintended way. I am building a webapi, to learn backend, using postman as the front end to complete CRUD operations on a database. I have had issues writing the methods, cause I am new to coding, and when I tried debugging I may have deleted code VS code though was irrelevant in the using statements.

I have spent so much time working on figuring out the methods and chasing issues that I don't even have a clue what I may have deleted in the using statements, so I have no idea where to start.

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;

namespace L06HandsOn.Models
{
    public class Car
{
    internal static readonly IActionResult ToList;

    public int Id {get; set;}
    public int Year {get; set;}
    public string Make {get; set;}
    public string Model {get; set;}
    public int numberOfPassengers {get; set;}


    }
}

when I do a dotnet run it says Car.ToList is never assigned to and will always have its default value null. When I run postman, I always get a 500 internal server error, but I was getting that even when it did complete the operations.

EDIT* Here is the controller.

using Microsoft.AspNetCore.Mvc;
using L06HandsOn.Models;
using System.Collections.Generic;

namespace L06HandsOn.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class CarsController : ControllerBase
{
    private readonly CarsContext _context;

    public CarsController(CarsContext context)
    {
        _context = context;
    }

    // POST api/Cars
    [HttpPost]
        public IActionResult Post([FromBody] Car car )
        {
            if (ModelState.IsValid)
            {
                 _context.Add(car);
                 _context.SaveChanges();
            }
            return CreatedAtRoute("", car.Id, car);
        }
    // GET api/car(all)
    [HttpGet]
        public IActionResult Get()
        {
            return Ok(_context.Cars);
        }
    // GET api/car(by Id)
    [HttpGet("{id:int")]
        public IActionResult Details(int id)
        {
            return Ok(_context.Cars.Find(id));
        }
    //GET api/car(if numberOfPassengers<3)
    [HttpGet("{numberOfPassengers:int}")]
    [Route("Get3")]
        public IActionResult Details2(int numberOfPassenger)
        {
            return Ok(_context.Cars.Find(numberOfPassenger<3));
        }
    //GET api/car(Order by year DESC)
    /* [HttpGet("{year:int}")]
    [Route("YDESC")]
        public IActionResult Details3(int year)
        {
            return Ok(_context.Cars.Find(year.OrderBy.DESC));
        }*/
}

}

VT Chiew I only relate it because the error code I get reads

Models\Car.cs(9,48): warning CS0649: Field 'Car.ToList' is never assigned to, and will always have its default value null [C:\Users\Jon-Michael\Desktop\Programming\CSharp\L06HandsOn\L06HandsOn.csproj]

The code it refers to is Models\\Car.cs line 9 which is

internal static readonly IActionResult ToList;

which when hovered over does say IActionResult Car.ToList, that's why In my head it sounds like a using statement is missing that came in with dotnet new web api build out. I'm not afraid to admit I'm new and I'm learning, I don't know the right terminology or anything beyond how it makes sense in my head.

First of all, "internal static readonly" means that ToList is visible in assembly, static field are shared across all instance of class Car, and readonly is keyword which said that we can set value of field only in constructor of class. So, when we have static readonly, it means that you can set value just in static constructor of class Car. Finally, in this case, you do not need ToList field, it is safe to remove.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM