如何实例化c#类
我似乎无法让我的类实例化。当我运行我的web应用程序时。我试图将我的类的结果传递到一些google图表中,但似乎无法让类实例化。如有任何帮助/链接/代码,将不胜感激 这就是我似乎被卡住的地方: DashboardController.cs如何实例化c#类,c#,asp.net-mvc,C#,Asp.net Mvc,我似乎无法让我的类实例化。当我运行我的web应用程序时。我试图将我的类的结果传递到一些google图表中,但似乎无法让类实例化。如有任何帮助/链接/代码,将不胜感激 这就是我似乎被卡住的地方: DashboardController.cs using System.Web.Mvc; namespace AudAPIApp.Controllers { [Authorize] public class DashboardController : Controller {
using System.Web.Mvc;
namespace AudAPIApp.Controllers
{
[Authorize]
public class DashboardController : Controller
{
public ActionResult Index()
{
GoogleAPI googleReport = new GoogleAPI();
return View(googleReport);
//New to MVC not sure if this is how to instantiate my class?
}
}
}
我在我的类中放置了代码块,但我的应用程序没有在其中运行
GoogleApi.cs
namespace AudAPIApp
{
public class GoogleAPI
{
public static GetReportsResponse googleRpt;
static void Main(string[] args)
{
try
{
//code that gets all the data. This all works in a console app I made. My challenge is making it work in MVC.
var response = batchRequest.Execute();
googleRpt = response;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
Index.cshtml
<script>
var googleData = [];
@{
//this is where I get error: "Object reference not set to an instance of an object."
foreach(var x in GoogleAPI.googleRpt.Reports.First().Data.Rows)
{
@:googleData.push([@x.Dimensions, @x.Metrics])
}
}
</script>
var googleData=[];
@{
//这就是我得到错误的地方:“对象引用未设置为对象的实例。”
foreach(GoogleAPI.googleRpt.Reports.First().Data.Rows中的变量x)
{
@:googleData.push([@x.Dimensions,@x.Metrics])
}
}
我知道这也不应该起作用,因为它使用的是一个未实例化的对象。首先,因为这看起来像一个网站,所以不要使用Main,因为你没有一个控制台应用程序来连接它,也不要使它成为静态的。大概是这样的: 一般来说(可能过于简化),如果您的类只有静态的东西,那么您不需要实例化它(static=在类的所有实例之间共享,因此创建一个新实例不会做任何事情) } 接下来,更改控制器以创建该对象,调用generate函数并将其作为模型传递
using System.Web.Mvc;
namespace AudAPIApp.Controllers
{
[Authorize]
public class DashboardController : Controller
{
public ActionResult Index()
{
GoogleAPI googleReport = new GoogleAPI();
googleReport.GenerateReport();
return View(googleReport);
//New to MVC not sure if this is how to instantiate my class?
}
}
}
现在,您已经将一个名为googleReport的GoogleAPI实例传递到视图中,并且googleReport.googleRpt将填充您的报告
//Add this to the top of your cshtml file
@model AudAPIApp.GoogleAPI
<script>
var googleData = [];
@{
//this is where I get error: "Object reference not set to an instance of an object."
foreach(var x in Model.googleRpt.Reports.First().Data.Rows)
{
@:googleData.push([@x.Dimensions, @x.Metrics])
}
}
</script>
//将其添加到cshtml文件的顶部
@模型AudAPIApp.GoogleAPI
var googleData=[];
@{
//这就是我得到错误的地方:“对象引用未设置为对象的实例。”
foreach(Model.googleRpt.Reports.First().Data.Rows中的变量x)
{
@:googleData.push([@x.Dimensions,@x.Metrics])
}
}
现在,您可以将报表作为模型对象的属性获取,模型对象实际上是您在控制器中创建的GoogleAPI对象。您在此处发布了大量代码。请参阅这篇关于如何创建示例的文章。所有方法的可能重复项都是
static
。如果希望将内容包含到特定实例中,请删除静态。您还有一个Main(args)
,这让我相信您是从控制台应用程序复制的。明智的做法是将Main
重命名为更具描述性的名称。还有一个问题是,您的Index.cshtml
iis使用的是静态类,而不是您创建的实例。创建对象时,不会调用GoogleAPI
中的主函数。要么将其移动到构造函数中,要么将其放入手动调用的初始化方法中。此外,View(param)希望视图中可以使用的模型对象附加到属性。它不会运行Main,或者实际上除了将该对象传递给razor视图(Index.cshtml)之外,它不会为您做任何事情。然后可以使用@model GoogleApi
并引用googleReport
对象的属性。但正如其他人提到的,一切都是静态的,所以现在这对你来说是相当无用的。这就成功了!谢谢你解释它是如何工作的。我感谢你的帮助。
//Add this to the top of your cshtml file
@model AudAPIApp.GoogleAPI
<script>
var googleData = [];
@{
//this is where I get error: "Object reference not set to an instance of an object."
foreach(var x in Model.googleRpt.Reports.First().Data.Rows)
{
@:googleData.push([@x.Dimensions, @x.Metrics])
}
}
</script>