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

时间:2016-02-16 21:48:51

标签: c# azure asp.net-web-api path

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

项目结构: http://imgur.com/HX4kDwF

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;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您是否将excel文件包含在项目中? (构建后,检查bin文件夹)。如果没有,请包括您的excel文件。

部署到Azure(我假设为Azure App Service)时,文件的绝对路径为

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