使用Web API在布局页面上输入C#

本文关键字:输入 布局 Web API 使用 | 更新日期: 2024-10-21 02:56:30

我在Console中编写了以下代码。我想像页面上的任何其他东西一样,在Layout.cshtml页面上显示代码返回的字符串。我如何使用Web API?

 namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string serverName = "localhost";
            var nsm = new ServerManager();
            using (Microsoft.Web.Administration.ServerManager sm = Microsoft.Web.Administration.ServerManager.OpenRemote(serverName))
            {
               //site mySite = sm.Sites.Add("Racing Cars Site", d:''inetpub''wwwroot'racing",  8080);
                int counter = 1; 
                foreach (var site in sm.Sites)
                {
                    //var p = site.Bindings.GetCollection().GetAttribute("physicalPath");
                    var p = site.Applications[0].VirtualDirectories[0].PhysicalPath;
                      int b=0;
                    foreach (Microsoft.Web.Administration.Binding binding in site.Bindings)
                         b= binding.EndPoint.Port;
                    Console.Write(String.Format(CultureInfo.InvariantCulture
                        , "Site number {0} : , {1} PhysicalPath : {2}  , Port:{3} {4} "
                        , counter.ToString(), site.Name ,p , b, Environment.NewLine));
                    counter++; 
                }
                Console.ReadKey();
            }
        }
    }
}

使用Web API在布局页面上输入C#

您的需求与Web API无关,请忘记这一点。ASP.NET MVC应用程序是可行的。正如您所说,它可以使用助手来完成,但有更简单的方法可以做到这一点。(使用模型或Viewbag)。您还需要学习如何使用视图中使用的渲染引擎的标记,如Razor。我建议您阅读一些教程,从这里开始阅读您的问题,在这里阅读

如果要在MVC中使用这些代码,则需要创建MVC应用程序。在这种情况下,没有其他方法可以做到这一点。

尝试在MVC中重写它。顺便说一句,如果你想在console application中显示输出,你必须使用Debug

以下是示例:

System.Diagnostics.Debug.WriteLine("The String");

意味着debugMVC中的console码。

更新:如果你想在网页上显示你的代码的结果,你必须在MVC应用程序中重写你的代码。

你还问你是否想使用helpers,所以我应该说你肯定可以使用MVC助手。

希望能有所帮助。