[英]Using shared view in ASP.NET MVC 4
我试图将共享视图添加到“ 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
如果要返回纯文本,则最好使用ContentResult
。 这是专门用于返回原始内容而无需通过视图引擎的操作,默认情况下,其内容类型为text/html
。
有关更多详细信息,请参见https://stackoverflow.com/a/553952/493650 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.