Razor 我有从服务引用API获取数据的方法。如何让数据显示在剃须刀页面上?

Razor 我有从服务引用API获取数据的方法。如何让数据显示在剃须刀页面上?,razor,asp.net-core,Razor,Asp.net Core,我有一个运行正常的.NETCore2.1控制台应用程序,可以在屏幕上显示字符信息。数据是从调用API的服务引用中收集的 我现在想把它变成一个web应用程序。因此,我在Visual Studio 2017中使用Razor(cshtml)创建了一个新的.NET Core 2.1 Web应用程序 我添加了控制台应用程序中的所有方法和服务引用,并消除了所有错误 所以我现在有了一个可以构建的web应用程序,但它只显示了在我创建项目时自动创建的“虚拟ASP.NET核心示例数据”页面 但我不确定如何获得显示数

我有一个运行正常的.NETCore2.1控制台应用程序,可以在屏幕上显示字符信息。数据是从调用API的服务引用中收集的

我现在想把它变成一个web应用程序。因此,我在Visual Studio 2017中使用Razor(cshtml)创建了一个新的.NET Core 2.1 Web应用程序

我添加了控制台应用程序中的所有方法和服务引用,并消除了所有错误

所以我现在有了一个可以构建的web应用程序,但它只显示了在我创建项目时自动创建的“虚拟ASP.NET核心示例数据”页面

但我不确定如何获得显示数据的方法。以前,我只会使用“Console.WriteLine”,如下所示:

    public static void PrintCharacter(Character pc)
    {
        Console.WriteLine("Character Name =  " + pc.Name);
        ListCharactersResponse response = GetCharacter(pc.Id);

        foreach (Attribute attr in response.Results)
        {
            Console.WriteLine("Character Attributes: " + attr.Strength + " | " + attr.Intelligence + " | " + GetMagicPoints(attr.Intelligence, attr.Wisdom));
        }

    }
public class AboutModel : PageModel
{
    public string Message { get; set; }

    public void OnGet()
    {
        Message = "Your application description page.";
    }
}
通过查看生成的示例页面,我看到如下内容:

    public static void PrintCharacter(Character pc)
    {
        Console.WriteLine("Character Name =  " + pc.Name);
        ListCharactersResponse response = GetCharacter(pc.Id);

        foreach (Attribute attr in response.Results)
        {
            Console.WriteLine("Character Attributes: " + attr.Strength + " | " + attr.Intelligence + " | " + GetMagicPoints(attr.Intelligence, attr.Wisdom));
        }

    }
public class AboutModel : PageModel
{
    public string Message { get; set; }

    public void OnGet()
    {
        Message = "Your application description page.";
    }
}
我不太确定如何将数据从方法(如上面的PrintCharacter)中获取到.cshtml页面中

我还需要Visual Studio做其他事情吗?或者我是否需要编写某种类型的方法来在网页上而不是控制台上显示数据


谢谢

在Razor页面中,您只需在视图中使用
@Model.Message
即可显示直接在PageModel中定义的变量。
ViewData
处于启用状态,而不允许使用
ViewBag
。请参阅

看法

@page
@关于模型的模型
@ViewData[“消息”]
@模型消息
更新:

要从视图中的静态方法读取数据,您需要从该方法返回数据,并知道要调用它的名称空间。参考

例如,在ConsoleApp类中创建一个静态方法

public class ConsoleClass
{
    public static List<string> PrintCharacter(Character pc)
    {
        Console.WriteLine("Character Name =  " + pc.Name);
        ListCharactersResponse response = GetCharacter(pc.Id);
        List<string> Test = new List<string>();

        foreach (Attribute attr in response.Results)
        {
            Test.Add("Character Attributes: " + attr.Strength + " | " + attr.Intelligence + " | " + GetMagicPoints(attr.Intelligence, attr.Wisdom));
        }
        return Test;
   }
}
公共类控制台类
{
公共静态列表打印字符(字符pc)
{
Console.WriteLine(“Character Name=“+pc.Name”);
ListCharactersResponse-response=GetCharacter(pc.Id);
列表测试=新列表();
foreach(response.Results中的属性attr)
{
测试.添加(“角色属性:+attr.Strength+“|”+attr.Intelligence+“|”+GetMagicPoints(attr.Intelligence,attr.willigence));
}
回归试验;
}
}
在PageModel中,我们需要调用该方法:

[BindProperty]
public Character Character { get; set; }
public List<string> DataStored { get; set; }

public IActionResult OnGet()
{
    DataStored = ConsoleApp.ConsoleClass.PrintCharacter(Character);//use correct namespace
}
[BindProperty]
公共字符{get;set;}
公共列表数据存储{get;set;}
公共IActionResult OnGet()
{
DataStored=ConsoleApp.ConsoleClass.PrintCharacter(Character);//使用正确的命名空间
}
鉴于:

@foreach (var item in Model.DataStored)
{
   <h3>@item</h3>
}
@foreach(Model.DataStored中的var项)
{
@项目
}
您也可以在视图中直接调用静态方法,如:

@foreach (var item in ConsoleApp.ConsoleClass.PrintCharacter(Model.Character))
{
   <h3>@item</h3>
}
@foreach(ConsoleApp.ConsoleClass.PrintCharacter(Model.Character)中的变量项)
{
@项目
}

谢谢,但我想问一下如何从PrintCharacter方法中获取数据以显示在网站上。谢谢,我迟到了。我更新了回复,并在consoleApp中测试了调用方法。