简体   繁体   English

在ASP.NET MVC 4中使用共享视图

[英]Using shared view in ASP.NET MVC 4

I tried to add a shared view to Views/Shared folder which I need to return some plain text values in some cases. 我试图将共享视图添加到“ Views/Shared文件夹,在某些情况下,我需要返回一些纯文本值。 Here is my shared view TextPlainView : 这是我的共享视图TextPlainView

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

I tried to use it from a controller this way: 我尝试通过这种方式从控制器使用它:

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

I want to use this view across all controllers, so I want it to be shared. 我想在所有控制器上使用此视图,因此我希望将其共享。

I got this error: 我收到此错误:

The view 'TextPlainView' or its master was not found or no view engine supports the searched locations. 找不到“ TextPlainView”视图或其主视图,或者没有视图引擎支持搜索到的位置。 The following locations were searched: ~/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 搜索以下位置:〜/ 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

If you want to return plain text it would probably be better to use a ContentResult . 如果要返回纯文本,则最好使用ContentResult This is specifically for returning raw content without going through a view engine and by default it has a content type of text/html . 这是专门用于返回原始内容而无需通过视图引擎的操作,默认情况下,其内容类型为text/html

See https://stackoverflow.com/a/553952/493650 for more details. 有关更多详细信息,请参见https://stackoverflow.com/a/553952/493650

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM