【问题标题】:Using shared view in ASP.NET MVC 4在 ASP.NET MVC 4 中使用共享视图
【发布时间】:2014-03-05 22:54:24
【问题描述】:

我尝试向Views/Shared 文件夹添加共享视图,在某些情况下我需要返回一些纯文本值。这是我的共享观点TextPlainView

@model string
@{
    Layout = null;
    Response.ContentType = "text/plain";
}
@Model

我尝试通过这种方式从控制器中使用它:

public ActionResult GetInfo(bool plain)
{
    //Some code to prepare data
    string result="Some data";
    if (plain)
        return View("TextPlainView", result);
    else
        return View(result);
}

我想在所有控制器中使用这个视图,所以我希望它被共享。

我收到了这个错误:

未找到视图“TextPlainView”或其主视图,或者没有视图引擎支持搜索到的位置。搜索了以下位置: ~/Views/Orders/TextPlainView.aspx ~/Views/Orders/TextPlainView.ascx ~/Views/Shared/TextPlainView.aspx ~/Views/Shared/TextPlainView.ascx ~/Views/Orders/0.master ~/Views/Shared/0.master ~/Views/Orders/TextPlainView.cshtml ~/Views/Orders/TextPlainView.vbhtml ~/Views/Shared/TextPlainView.cshtml ~/Views/Shared/TextPlainView.vbhtml ~/Views/Orders/0.cshtml ~/Views/Orders/0.vbhtml ~/Views/Shared/0.cshtml ~/Views/Shared/0.vbhtml

【问题讨论】:

标签: c# asp.net-mvc asp.net-mvc-4 shared


【解决方案1】:

如果您想返回纯文本,最好使用ContentResult。这专门用于在不通过视图引擎的情况下返回原始内容,默认情况下它的内容类型为text/html

更多详情请见https://stackoverflow.com/a/553952/493650

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    相关资源
    最近更新 更多