gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:29:25 26 4
gpt4 key购买 nike

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

项目结构: /image/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;
}
}
}

最佳答案

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

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

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

关于c# - ASP.NET Web 应用程序文件路径(在 Azure 上发布),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35443680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com