Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 在MVC中读取文本文件_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 在MVC中读取文本文件

Asp.net mvc 在MVC中读取文本文件,asp.net-mvc,Asp.net Mvc,我最近尝试从一个文本文件中读取并填充数据库,然后将填充的数据显示到我的MVC应用程序中。所有这些都是因为我希望我的桌面应用程序能够将数据传递给我的MVC应用程序,并将其保存到我的MVC数据库中 这就是我所拥有的: public ActionResult Index([Bind(Include = "location_id,location_name")] Location location) { string full_path = Request.MapPath("~/

我最近尝试从一个文本文件中读取并填充数据库,然后将填充的数据显示到我的MVC应用程序中。所有这些都是因为我希望我的桌面应用程序能够将数据传递给我的MVC应用程序,并将其保存到我的MVC数据库中

这就是我所拥有的:

public ActionResult Index([Bind(Include = "location_id,location_name")] Location location)
    {
        string full_path = Request.MapPath("~/App_Data/TestText.txt");
        if (System.IO.File.Exists(full_path))
        {
            if (new System.IO.FileInfo(full_path).Length > 0)
            {
                string[] text_file = System.IO.File.ReadAllLines(HostingEnvironment.MapPath(@"~/App_Data/TestText.txt"));
                foreach (string record in text_file)
                {
                    string[] record_cell = record.Split('~');
                    location.location_name = record_cell[1];
                    db.Locations.Add(location);
                    db.SaveChanges();
                }

                System.IO.File.WriteAllText(HostingEnvironment.MapPath(@"~/App_Data/TestText.txt"), "");
            }
        }

        ViewBag.location_id = new SelectList(db.Locations, "location_id", "location_name");

        return View();
    }
上面的代码放在我的主控制器中,这样每次页面加载时,它都会查看文本文件中的新数据并将其存储在db中,同时返回一个视图以显示索引页面

在localhost上一切正常,但在托管时,出现以下错误:

我把我的文本文件放在App_数据中,但是我觉得把代码放在控制器的索引下会导致这种情况。你能帮我告诉我在哪里放置代码,以便在网站启动时检查文本文件中的更新数据吗

更新:

我发现了导致网站无法加载的错误。不知何故,视图中用db数据填充下拉列表的以下代码行不起作用:

@Html.DropDownList("location_id", null, htmlAttributes: new { @class = "form-control"})

有解决方案吗?

您需要调试应用程序,并告诉我们它在哪一行给出的确切错误。这不是一个明确的问题陈述,它只会导致猜测。您可以使用断点和catch块来显示发生错误的堆栈跟踪和行位置。然后,把它包括在你的问题中。