0

I got a error (CS0103 C# The name 'Content' does not exist in the current context) in code below:

public ActionResult getsomething()
{
    var res = "something";
    return Content(res);}

Do u have any idea i can fix that?

Anamnian
  • 417
  • 4
  • 20

2 Answers2

1

Try use System.Web.Mvc.Controller Content() function instead System.Web.UI.WebControls.Content Class

EDIT:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication2.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return Content("xxxxx");
        }
    }
}

Its work fine
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

Narek Arzumanyan
  • 616
  • 3
  • 11
0

Edit: My bad, not used to ASP.NET. Will keep my previous answer for transparency

Seeing this answer here, use ContentResult instead of ActionResult

public ContentResult getsomething()
{
    var res = "something";
    return Content(res);
}

Original post:

Did you try adding the new keyword ? Content is a class, thus need to be initialized.

public ActionResult getsomething()
{
    var res = "something";
    return new Content(res);
}
Community
  • 1
  • 1
Beltaine
  • 2,721
  • 1
  • 18
  • 22
  • using System.Web.UI.WebControls; public ActionResult getsomething() { var res = "something"; return new Content(res); } It says 'Content' doesnt have a constructor that takes 1 argument – Anamnian Aug 04 '16 at 12:29
  • 1
    There is no need to initialise *Content* in the return statement. – Romy Mathews Aug 04 '16 at 12:32
  • Try to use `ContentResult` instead of `ActionResult` [Solution here](http://stackoverflow.com/a/553952/5785572) – Beltaine Aug 04 '16 at 12:34
  • (Edit) Try use ContentResult, got this error: Non-invocable member 'Content' cannot be used like a method. – Anamnian Aug 04 '16 at 12:39
  • Did you remove the `new` and remove the `using` ? (I induced you in error, I'm sorry) – Beltaine Aug 04 '16 at 12:42
  • Sure. public ContentResult getsomething() { var res = "something"; return Content(res); } – Anamnian Aug 04 '16 at 12:43