当前上下文中不存在内容

本文关键字:不存在 上下文 | 更新日期: 2023-09-27 18:00:07

我在下面的代码中得到一个错误(CS0103 C#名称"Content"在当前上下文中不存在):

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

你知道我能修好吗?

当前上下文中不存在内容

尝试使用System.Web.Mvc.Controller Content() function而不是System.Web.UI.WebControls.Content Class

编辑:

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");
        }
    }
}

它运行良好
======全部重建:1成功,0失败,0跳过======

编辑:我不好,不习惯ASP.NET。将保留我以前对透明度的回答

看到这里的答案,请使用ContentResult而不是ActionResult

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

原帖:

您是否尝试添加new关键字?内容是一个类,因此需要初始化。

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