Computer Guy
Computer Guy

Reputation: 411

from Console to Web-App

What would be a 'non-console application' representation of the following code.

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

I can't seem to figure out what this code would look like in a C# ASP.NET MVC project.

Upvotes: 0

Views: 153

Answers (4)

Yuriy Guts
Yuriy Guts

Reputation: 2220

You can create a SwordController with the action Hit:

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

If you access the page using this URL: http://[domain]/Sword/Hit?target=watermelon you'll see this string in your Web browser: Chopped watermelon in half.

Upvotes: 3

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25445

In ASP.net you would do something like this

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

Upvotes: 1

Icarus
Icarus

Reputation: 63956

On an Web app, your "Console" is your HTTP Response; therefore, that piece of code on a web app, would look like this:

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

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

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

and then you could have a controller:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        var sword = new Sword();
        return View((object)sword.Hit("foo"));
    }
}

and a corresponding view:

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

I have strictly no idea why your question is tagged with Ninject, but if you want to use Ninject in an ASP.NET MVC application you may install the Ninject.MVC3 NuGet package and go through some tutorials such as this one for example.

Upvotes: 5

Related Questions