2012-02-26 172 views
0

以下代码的“非控制台应用程序”表示形式是什么?从控制台到Web应用程序

class Sword 
    { 
     public void Hit(string target) 
     { 
      Console.WriteLine("Chopped {0} clean in half", target); 
     } 
    } 

我似乎无法弄清楚这段代码在C#ASP.NET MVC项目中的样子。如果你

@model string 
<div>@Model</div> 

我有严格不知道为什么你的问题被打上Ninject,但:

回答

5
class Sword 
{ 
    public string Hit(string target) 
    { 
     return string.Format("Chopped {0} clean in half", target); 
    } 
} 

,然后你可以有一个控制器:

和相应的视图想要在ASP.NET MVC应用程序中使用Ninject,您可以安装Ninject.MVC3 NuGet包,并通过一些教程,例如this one

+0

我提到ninject,因为它是从[他们的页面]复制的(https://github.com/ninject/ninject/wiki/Dependency-Injection-By-Hand)。并删除它,因为它显然惹恼你(因为你的权利(我承认))。 – 2012-02-26 20:53:15

1

在Web应用程序上,您的“控制台”是您的HTTP响应;因此,在一个Web应用程序是一段代码,应该是这样的:

class Sword 
{ 
    public void Hit(string target) 
    { 
     Response.Write(string.Format("Chopped {0} clean in half", target)); 
    } 
} 
+0

这是不正确的。抛出以下错误:由于'Samurai.Models.Sword.Hit(string)'返回void,因此返回关键字后面不能有对象表达式 – 2012-02-26 22:17:02

1

在ASP.net你会做这样的

Response.Write(string.format("Chopped {0} clean in half", target); 
+0

这是错误的。抛出以下错误:由于'Samurai.Models.Sword.Hit(string)'返回void,所以返回关键字后面不能有对象表达式 – 2012-02-26 22:18:32

3

东西,你可以创建一个动作Hit一个SwordController

public class SwordController : Controller 
{ 
    public ActionResult Hit(string target) 
    { 
     return Content(string.Format("Chopped {0} in half", target)); 
    } 
} 

如果您访问使用此网址的页面:http://[domain]/Sword/Hit?target=watermelon你会看到这个字符串在Web浏览器:Chopped watermelon in half

相关问题