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

C# input on Layout page using Web API

我在控制台中编写了以下代码。我想在我的 Layout.cshtml 页面上显示字符串代码 returns,就像页面上的任何其他内容一样。我如何使用 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,别提了。 ASP.NET MVC 应用程序是可行的方法。 正如您所说,可以使用助手来完成,但存在更简单的方法。 (使用模型或 Viewbag)。您还需要学习如何使用视图中使用的渲染引擎的标记,例如 Razor。我建议你阅读一些教程,从 here for your issue, and here

开始

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

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

示例如下:

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

也就是说debug就是MVC中的console代码。

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

你还问要不要用helpers,所以我应该说你绝对可以用MVC helpers。

希望对您有所帮助。