简体   繁体   English

使用ASP.NET Core从MS EntityFrameworkCore中的Json文件获取记录

[英]Get records from Json file in MS EntityFrameworkCore using asp.net core

There is a JSON file on the desktop. 桌面上有一个JSON文件。 I want this API to grab that file and then add all the records within that file ready to display and for requests. 我希望该API能够抓取该文件,然后在该文件中添加所有记录,以供显示和请求。

right now it is importing everything from the JSON file into an object but I cannot go any further. 现在它正在将所有内容从JSON文件导入到对象中,但是我无法再进行任何操作了。

I am new to APIs and ASP.Net 我是API和ASP.Net的新手

Can anyone help me please. 谁能帮我。

Thank you 谢谢

[Route("api/[controller]")]
public class TodoController : Controller
{

    private readonly TodoContext _context;
    public TodoController(TodoContext context)
    {

        _context = context;
        if (_context.TodoItems.Count() == 0)
        {
            string allText = System.IO.File.ReadAllText(@"C:\Users\Alex\Desktop\Prog\A.json");
            object jsonObject = JsonConvert.DeserializeObject(allText);

            int x = jsonObject.
            _context.TodoItems.Add(new TodoItems { Id = '1', Name = "Item1", IsComplete = false,  });
            _context.SaveChanges();
        }
    }

You have written the code in constructor, its not a good approach to write in that way. 您已经在构造函数中编写了代码,这不是以这种方式编写的好方法。 Probably what you can do is add GET/POST method depending on your requirement. 可能您可以根据需要添加GET / POST方法。 I have made a GET api call to execute your case.Please check below code snippet. 我已经执行了GET api调用来执行您的案例。请检查以下代码段。

            [Produces("application/json")]
            [Route("api/TodoController")]
            public class TodoControllerController : Controller
            {
                private readonly TodoContext _context

                public void Get()
                {
                    _context = context;
                    if (_context.TodoItems.Count() == 0)
                    {
                        string allText = System.IO.File.ReadAllText(@"C:\Users\Alex\Desktop\Prog\A.json");
                        object jsonObject = JsonConvert.DeserializeObject(allText);

                        int x = jsonObject.
                        _context.TodoItems.Add(new TodoItems { Id = '1', Name = "Item1", IsComplete = false, });
                        _context.SaveChanges();
                    }
                }
            }
[Produces("application/json")]
[Route("api/ToDo")]
public class ToDoController : Controller
{
    private TodoContext _context;
    public TodoController(TodoContext context)
    {
        _context = context;
    }

    [HttpPost]
    [Route("Upload")]
    public HttpResponseMessage UploadData()
    {
        try
        {
            if (_context.TodoItems.Count() == 0)
            {
                string allText = System.IO.File.ReadAllText(@"C:\Users\Alex\Desktop\Prog\A.json");
                object jsonObject = JsonConvert.DeserializeObject(allText);

                int x = jsonObject.
                _context.TodoItems.Add(new TodoItems { Id = '1', Name = "Item1", IsComplete = false, });
                _context.SaveChanges();
            }
        }
        catch (Exception ex)
        {
            return new HttpResponseMessage(HttpStatusCode.InternalServerError);
        }
    }
}

Whenever a service call comes for the controller. 每当有服务呼叫控制器时。 On that time It will invoke the constructor. 届时它将调用构造函数。 Instead of this, if we move the code to a separate method and invoke this method when required. 取而代之的是,如果我们将代码移至单独的方法并在需要时调用此方法。 It will increase the performance and also remove any calls to invoke the constructor unwantedly. 它将提高性能,并删除所有不必要地调用构造函数的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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