skip to Main Content

I created a project using the cmd line:

dotnet new web -o TodoApi

I then created . ./Models/Todo.cs class, and I would like to import it in my ./Program.cs file at the root of my project.

this is my class file:

using System;

namespace TodoApi.Models
{
    public class Todo
    {
        public string Description { get; set; }
        public bool IsCompleted { get; set; }
    }
}

It seems using the import using TodoApi.Models in the Program.cs doesn’t do anything.

How should I go about being able to do some import in such a project creation?

I may say that the code compiles and builds, it works. But in Visual Studio 2022 IDE, I don’t get suggestions when I start typing the using TodoApi... and if I use the class in the Program file, it is not recognized as being a type with set attributes…

2

Answers


  1. Chosen as BEST ANSWER

    So I believe I was doing everything right, and the issue was just the IDE, Visual Studio, "issue". After getting back to it (refreshing Visual Studio), my Todo class in my file was recognized.

    So by using the namespace and the imports properly, you may need to restart your application for it to be recognized as such.

    Thanks for the input!


  2. To access your class in the program file you have to append the whole namespace before the class like:

    TodoApi.Models.Todo Todo = new TodoApi.Models.Todo();
    

    If that’s too long to write, use namespace ALIASES by importing the class at the top like:

    using Todo = TodoApi.Models.Todo;
    

    This way you can just write Todo class in your program file without appending the namespace to it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search