Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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# ASP.NET Web应用程序文件路径(在Azure上发布)_C#_Azure_Asp.net Web Api_Path - Fatal编程技术网

C# ASP.NET Web应用程序文件路径(在Azure上发布)

C# ASP.NET Web应用程序文件路径(在Azure上发布),c#,azure,asp.net-web-api,path,C#,Azure,Asp.net Web Api,Path,问题:对于一个学校项目,我创建了一个RESTWeb服务,该服务与一个数据库链接,该数据库从excel文件获取他的数据。在Migration/Configuration.cs类中,我读取excel文件并将其放入数据库。但是当我试图在Azure上发布我的项目时,他似乎找不到excel文件,我完全不知道excel文件的路径应该是什么。(当我将Configuration.cs的seed方法更改为一些硬编码的东西时,它确实起作用)。我已经尝试更改到Server.MapPath或HttpRuntime.Ap

问题:对于一个学校项目,我创建了一个RESTWeb服务,该服务与一个数据库链接,该数据库从excel文件获取他的数据。在Migration/Configuration.cs类中,我读取excel文件并将其放入数据库。但是当我试图在Azure上发布我的项目时,他似乎找不到excel文件,我完全不知道excel文件的路径应该是什么。(当我将Configuration.cs的seed方法更改为一些硬编码的东西时,它确实起作用)。我已经尝试更改到Server.MapPath或HttpRuntime.AppDomainAppPath的路径,但它不起作用。。。有人知道如何解决这个问题吗?非常感谢

项目结构:

Configuration.cs:

namespace RestServiceTest.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.IO;
    using System.Linq;
    using System.Web;
    using WineFoodModel;
    internal sealed class Configuration : DbMigrationsConfiguration<RestServiceTest.Models.WineRestTestContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(RestServiceTest.Models.WineRestTestContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
            string path = "C:\\Users\\Chen\\Documents\\School\\Eindwerk\\afstudeerproject\\RestServiceTest\\RestServiceTest\files\\Virtuele Sommelier juli 2015.xlsx";
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@path);
            Microsoft.Office.Interop.Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheet.UsedRange;

            int rCount = xlRange.Rows.Count;
            int cCount = xlRange.Columns.Count;

            string stringRange = "A1:L" + rCount;
            object[,] objectArray = xlWorksheet.get_Range(stringRange).Value2;

            for (int i = 1; i <= rCount; i++)
            {
                SubCategory subCategory = new SubCategory((string)objectArray[i, 2], new Category((string)objectArray[i, 1]));
                Food food = new Food((string)objectArray[i, 3], subCategory);

                WineFoodModel.Type type = (WineFoodModel.Type)Enum.Parse(typeof(WineFoodModel.Type), (string)objectArray[i, 4], true);
                string naamWijn = (string)objectArray[i, 5];
                string url = (string)objectArray[i, 6];
                double price = (double)objectArray[i, 7];
                Region region = new Region((string)objectArray[i, 9], new Country((string)objectArray[i, 8]));
                string appelatie = (string)objectArray[i, 10];
                bool bio = checkBio((string)objectArray[i, 11]);
                string description = (string)objectArray[i, 12];

                //Wine wine = new Wine(type, naamWijn, url, price, description, region, appelatie, bio);
                var wine = new Wine
                {
                    Appelatie = appelatie,
                    Bio = bio,
                    Description = description,
                    Name = naamWijn,
                    PictureHtml = url,
                    Price = price,
                    Region = new Region
                    {
                        Name = objectArray[i, 9] as string,
                        Country = new Country
                        {
                            Name = objectArray[i, 8] as string
                        }
                    },
                    WineType = type
                };
                context.Wines.AddOrUpdate(p => p.Name,
                    wine);
                //Salade    Zomerse salade  Zomerse salade  Wit 3P Picpoul de Pinet http://u.jimdo.com/www70/o/s144a62240ee1d941/img/i2876998c8f7cd492/1413374802/orig/image.jpg     8.50 €     Frankrijk   Languedoc   Picpoul de Pinet        De druif Picpoul de Pinet geeft  pittig en verfrissend wijnen met veel citrus, gele pruimen, abrikozen, bloesems en een goed gedefinieerde mineraliteit.  Perfecte zomerse verfrisser en dé zuid-Franse wijn voor bij uw Vis - Schaaldieren en mosselen gerechten.                                                                  
            }
        }
        public static bool checkBio(string text)
        {
            if (text != null && text.Equals("Y"))
            {
                return true;
            }
            return false;
        }
    }
}
namespace RestServiceTest.Migrations
{
使用制度;
使用System.Data.Entity;
使用System.Data.Entity.Migrations;
使用System.IO;
使用System.Linq;
使用System.Web;
使用WineFoodModel;
内部密封类配置:DBMigOptionsConfiguration
{
公共配置()
{
AutomaticMiggerationsEnabled=假;
}
受保护的覆盖无效种子(RestServiceTest.Models.WineRestTestContext)
{
//迁移到最新版本后将调用此方法。
//您可以使用DbSet.AddOrUpdate()助手扩展方法
//避免创建重复的种子数据。
//
//context.People.AddOrUpdate(
//p=>p.FullName,
//新人{FullName=“安德鲁·彼得斯”},
//新人{FullName=“Brice Lambson”},
//新人{FullName=“Rowan Miller”}
//    );
//
string path=“C:\\Users\\Chen\\Documents\\School\\Eindwerk\\afstudeerproject\\RestServiceTest\\RestServiceTest\files\\Virtuele斟酒师juli 2015.xlsx”;
Microsoft.Office.Interop.Excel.Application xlApp=新的Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xlWorkbook=xlApp.Workbooks.Open(@path);
Microsoft.Office.Interop.Excel.Worksheet xlWorksheet=xlWorkbook.Sheets[1];
Microsoft.Office.Interop.Excel.Range xlRange=xlWorksheet.UsedRange;
int rCount=xlRange.Rows.Count;
int cCount=xlRange.Columns.Count;
字符串stringRange=“A1:L”+rCount;
object[,]objectArray=xlsheet.get_Range(stringRange).Value2;
对于(int i=1;i p.名称,
葡萄酒);
//佐梅塞沙拉佐梅塞沙拉佐梅塞沙拉,配皮涅特皮普尔3便士http://u.jimdo.com/www70/o/s144a62240ee1d941/img/i2876998c8f7cd492/1413374802/orig/image.jpg     8.50欧元Frankrijk Languedoc Pinet de Pinet de druif Pinet geeft pittig en verfrissend wijnen会见了veel citrus、gele pruimen、abrikozen、bloesems en goe这是一种矿物,它是一种完美的矿物,它是一种天然的矿物。
}
}
公共静态bool checkBio(字符串文本)
{
if(text!=null&&text.Equals(“Y”))
{
返回true;
}
返回false;
}
}
}

是否将excel文件作为项目的一部分?(生成后,检查您的bin文件夹)。如果没有,请包括您的excel文件

当部署到Azure(我假设是Azure应用程序服务)时,您的文件的绝对路径为

Environment.ExpandEnvironmentVariables(@"%HOME%\site\wwwroot\{relative file path}")

您可以使用Server.MapPath发布您的尝试吗?显然,Azure中不存在硬编码的路径,对吗?阅读本文,您应该能够理解:构建后,我在bin文件夹中找不到我的excel文件,尽管我将其作为现有项添加到了我的项目中……您不希望该路径在本地以及应用程序服务上工作吗?这似乎不正确,除非您在本地强制IIS。。。。