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

本文关键字:Azure Web NET 应用程序 文件 路径 ASP | 更新日期: 2023-09-27 18:34:57

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

项目结构:https://i.stack.imgur.com/x3sg3.jpg

配置.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;
        }
    }
}

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

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

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

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