Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.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 api中读取json文件_C#_Asp.net_Asp.net Web Api - Fatal编程技术网

C# 如何在asp.net web api中读取json文件

C# 如何在asp.net web api中读取json文件,c#,asp.net,asp.net-web-api,C#,Asp.net,Asp.net Web Api,在我的asp.WebAPI2.0项目中,我有一个Json文件,其中映射了所有错误代码。我想读取json文件,以便将响应返回给调用方 我无法阅读相同的内容,但是如果我使用控制台应用程序来执行以下代码,任何建议都会有所帮助 在控制台应用程序中工作的代码: var assembly = Assembly.GetExecutingAssembly(); using (var stream = new StreamReader(assembly.GetManifestResourceStream("C

在我的asp.WebAPI2.0项目中,我有一个Json文件,其中映射了所有错误代码。我想读取json文件,以便将响应返回给调用方

我无法阅读相同的内容,但是如果我使用控制台应用程序来执行以下代码,任何建议都会有所帮助

在控制台应用程序中工作的代码:

 var assembly = Assembly.GetExecutingAssembly();
 using (var stream = new StreamReader(assembly.GetManifestResourceStream("ConsoleApp24.Utilities.StatusCodes.json") ?? throw new InvalidOperationException()))
 {
     var status = JsonConvert.DeserializeObject<RootObject>(stream.ReadToEnd());    
 }
var assembly=assembly.getExecutionGassembly();
使用(var stream=newstreamreader(assembly.GetManifestResourceStream(“ConsoleApp24.Utilities.StatusCodes.json”)??抛出新的InvalidOperationException())
{
var status=JsonConvert.DeserializeObject(stream.ReadToEnd());
}
使用上述代码在web api项目中将程序集设置为null,因此我将其更改为:

var assembly = GetWebEntryAssembly();
using (var stream = new StreamReader(assembly.GetManifestResourceStream("PaymentAccount.Api.Resources.StatusCodes.json") ?? throw new InvalidOperationException()))
{
     var status = JsonConvert.DeserializeObject<RootObject>(stream.ReadToEnd());       
}

private Assembly GetWebEntryAssembly()
{
     if (System.Web.HttpContext.Current == null ||
            System.Web.HttpContext.Current.ApplicationInstance == null)
     {
           return null;
     }

     var type = System.Web.HttpContext.Current.ApplicationInstance.GetType();
     while (type != null && type.Namespace == "ASP")
     {
           type = type.BaseType;
     }

     return type == null ? null : type.Assembly;
}
var assembly=GetWebEntryAssembly();
使用(var stream=newstreamreader(assembly.GetManifestResourceStream(“PaymentAccount.Api.Resources.StatusCodes.json”)?抛出新的InvalidOperationException())
{
var status=JsonConvert.DeserializeObject(stream.ReadToEnd());
}
私有程序集GetWebEntryAssembly()
{
if(System.Web.HttpContext.Current==null||
System.Web.HttpContext.Current.ApplicationInstance==null)
{
返回null;
}
var type=System.Web.HttpContext.Current.ApplicationInstance.GetType();
while(type!=null&&type.Namespace==“ASP”)
{
type=type.BaseType;
}
返回类型==null?null:type.Assembly;
}
我得到的例外是:

由于对象的当前状态,操作无效


使用
Server.MapPath
ASP.NET很容易找到您的文件,但文件仍必须位于应用程序根文件夹中,下面是有关此功能的一些官方信息

只需将文件放在根文件夹中,然后使用
Server.MapPath
这将允许ASP.NET应用程序在服务器文件系统中查找文件

string json = File.ReadAllText(Server.MapPath("~/files/myfile.json"));

您可以试试这个:

    public object Get()
{
    string allText = System.IO.File.ReadAllText(@"c:\data.json");

    object jsonObject = JsonConvert.DeserializeObject(allText);
    return jsonObject;
}

此代码返回json文本

我能够使用Server.MapPath解决它,尽管我将文件保存在不同的文件夹中。谢谢你的帮助。