Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.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
C# 获取错误';System.IO.FileNotFoundException';_C#_Asp.net Mvc_Asp.net Core - Fatal编程技术网

C# 获取错误';System.IO.FileNotFoundException';

C# 获取错误';System.IO.FileNotFoundException';,c#,asp.net-mvc,asp.net-core,C#,Asp.net Mvc,Asp.net Core,我试图遵循这一点,并试图部署带有asp.net核心web应用程序的ML模型,但当我运行它时,我得到了这个错误 An exception of type 'System.IO.FileNotFoundException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Could not find file '/Users/a10.12/Price Prediction/PricePredictio

我试图遵循这一点,并试图部署带有asp.net核心web应用程序的ML模型,但当我运行它时,我得到了这个错误

An exception of type 'System.IO.FileNotFoundException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Could not find file '/Users/a10.12/Price Prediction/PricePrediction/..\SampleRegression\SampleRegression.Model\MLModel.zip'.'
我的控制器是这个

using Microsoft.AspNetCore.Mvc;  
using Microsoft.ML;  
using SampleRegression.Model;  

 // 
namespace Price_Prediction.Controllers  
{  
    public class PredictionController : Controller  
    {  
        public IActionResult Price(ModelInput input)  
        {  
            // Load the model  
            MLContext mlContext = new MLContext();  
            // Create predection engine related to the loaded train model  
            ITransformer mlModel = mlContext.Model.Load(@"..\SampleRegression\SampleRegression.Model\MLModel.zip", out var modelInputSchema);  
            var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel); 



        // Try model on sample data to predict fair price  
        ModelOutput result = predEngine.Predict(input);  

        ViewBag.Price = result.Score;  
        return View(input);  
    }  
}  
首先,我认为这是因为权限问题,并试图运行它作为一个管理员,但仍然得到相同的错误,并试图改变控制器如下

(@"..\SampleRegression.Model\MLModel.zip", out var modelInputSchema);
但仍然没有结果,我做错了什么

更新: 我尝试使用绝对路径,如下所示:

ITransformer mlModel = mlContext.Model.Load(@"/Users/a10.12/Price Prediction/SampleRegression/SampleRegression.ConsoleApp/ModelBuilder.cs", out var modelInputSchema);
现在得到一个错误:

An exception of type 'System.FormatException' occurred in Microsoft.ML.Core.dll but was not handled in user code: 'Failed to open a zip archive'

尽管它使用的是绝对路径,但最好指出它不使用相对路径的原因是,当您运行代码时,编译后的代码驻留在应用程序的bin文件夹中。这意味着,当您执行
。\SampleRegression.Model\MLModel.zip
时,基本上是针对错误的文件夹

另外一个更好的解决方案是,不要使用绝对路径,而是将zip文件设置为在构建时复制到输出目录,然后从那里加载。这将使您的代码不那么容易出错

在输出目录中设置要复制的文件的方法是,在Visual Studio中右键单击该文件,然后将
复制到输出目录
设置为
始终复制
,或者如果不使用Visual Studio,则需要在csproj文件中设置该文件

<ItemGroup>
    <None Update="MLModel.zip">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
</ItemGroup>

总是

希望这能让事情变得更清楚

您的zip文件是否设置为在构建时复制?同样在更新部分的模型加载中,您尝试加载cs文件而不是zip文件。
<ItemGroup>
    <None Update="MLModel.zip">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
</ItemGroup>