Partager via


Controller.Content Method (String, String)

Creates a content result object by using a string and the content type.

Namespace:  System.Web.Mvc
Assembly:  System.Web.Mvc (in System.Web.Mvc.dll)

Syntax

'Declaration
Protected Friend Function Content ( _
    content As String, _
    contentType As String _
) As ContentResult
protected internal ContentResult Content(
    string content,
    string contentType
)
protected public:
ContentResult^ Content(
    String^ content, 
    String^ contentType
)

Parameters

  • content
    Type: System.String
    The content to write to the response.

Return Value

Type: System.Web.Mvc.ContentResult
The content result instance.

Remarks

The result object that is prepared by this method is written to the response by the MVC framework when the object is executed.

Examples

A Visual Studio project with source code is available to accompany this topic: Download.

The example in this section shows how to use markup and code to display "Use <b> for bold text" in the browser when the Test Content link is clicked.

The following example shows markup that invokes the TestContent method.

<%= Html.ActionLink("Test Content with Type", "TstContentConType", 
    new { id = false, sMsg="Use <b> for bold text"})%> 
<%= Html.ActionLink("Test Content with Type", "TstContentConType", 
    new { id = false, sMsg="Use <b> for bold text"})%> 

The following example shows the TestContent method.

[ValidateInput(false)]
public ActionResult TstContentConType(bool id, string sMsg) {
    string contentType = "text/plain";
    if (id == false)
        contentType = "text/html";
    return Content(Server.HtmlEncode(sMsg),contentType);
}
<ValidateInput(False)> _ 
Public Function TstContentConType(ByVal id As Boolean, ByVal sMsg As String) As ActionResult 
    Dim contentType As String = "text/plain" 
    If id = False Then 
        contentType = "text/html" 
    End If 
    Return Content(Server.HtmlEncode(sMsg), contentType) 
End Function 

See Also

Reference

Controller Class

Content Overload

System.Web.Mvc Namespace