Returning ContentResult in ASP.NET MVC

Previously, I wrote about returning JsonResult in ASP.NET MVC. Today, I write about returning ContentResult. So, what does ContentResult actually returns? It can return any content. It can be plain text, html, xml, etc. It is specified by the MIME type in the return statement.

Here’s a short demo to get you started:

Consider a jQuery AJAX call requesting some data:

$.ajax({

  // The url to which the request is to be sent.
  url: "http://localhost/myproject/home/givecontent",

  // The type of request. It can be "GET" or "POST" or others.
  type: "GET",

  // The data that is to be sent to the server. It can be omitted if no data is
  // to be sent. In a "GET" request this data is appended to the url.
  data: { "id": "foo", "name": "bar" },

  // The function to be called when the server returns some data. The data
  // variable holds the content that was returned.
  success: function (data) {
    document.write(data);
  },

  // Show an alert if an error occurs.
  error: function () {
    alert("Some error occured.");
  }
});

The above code will fire a request. Now let’s handle it in ASP.NET MVC. Consider a project named “myproject” containing a controller “homecontroller” and a method in the controller named “givecontent” (hence the url):

Imports System.Web.Mvc

Public Class HomeController
  Inherits Controller

  ' The sent data automatically maps to the function's arguments. Only the names
  ' must be same.
  Function givecontent(id As String, name As String) As ContentResult
    Return Content(id & " " & name, "text/plain")
    ' The second argument specifies the MIME type of the return value. It can be
    ' text/html, even text/xml, etc.
  End Function
End Class

The above would handle the request and return the plain text (which here is just the concantenation of the two arguments) to JavaScript which then writes it to document.

If you want to know more about MIME types, visit this link.

That’s it. If have any doubts relating to this, sound off in the comments below.

Advertisements

5 thoughts on “Returning ContentResult in ASP.NET MVC

  1. Hi dineshramatic,

    This is kind of what I need, but I need the reponse to be a Save As/Open dialog. From trying your code (using a mimetype of “application/msword”) it just opens the content in the browser window.

    What can I do?

    Thanks,
    Dave

    Like

    1. I didn’t exactly understand why you want to open a MS Word document in a browser but if you want the browser to download the file instead, you can use FileResult.
      It works the same way, the only difference being it returns a file to the browser which is then downloaded. You will have to return a file from the server.
      If your doubt is still not cleared, please elaborate it more and if possibe quote your piece of code.

      Regards,
      TurboBlaze

      Like

      1. Hi TurboBlaze,

        I managed to fix my problem.

        I had a “comments” text field with a button to export the text to a Word Document. I was trying to pass the comments back via ajax and dynamically create a Word document to return as the reponse. It didn’t work.

        Instead I created the URL in a javascript function, appended the comments to the query string, and used window.location.href = url.

        If that makes sense to you and others?

        Like

Your Thoughts -

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s