Practical .NET

Returning Simple Strings from Action Methods

When you want to return a string result -- either as text, HTML or XML -- then a ContentResult object gives you the right balance of simplicity and control.

Sometimes all you want to return from your Action method is some plain text (typically something like "Please go away" because something has gone horribly wrong). The easiest way to do that is to change the return value of your Action method to string and return your string:

Function GetCustomerInfo(Id As Integer?) As String
  Return "Please Go Away"
End Function

The problem is that you now have a method that can only return a string -- you can't, for example, return a View if everything goes well.

If you leave your return type as ActionResult, then you can use the Content helper method to return a ContentResult object. That gives you some extra support because the ContentResult object will specify the MIME type of the string you're returning. By default, that type is set to text/plain.

However, if you want to return some other type, you can specify that in the second parameter you pass to the Content method (the first parameter is the string you want to return).

For example, this code returns some emphatic text to the client as HTML when things go wrong:

Function GetCustomerInfo(Id As Integer?) As ActionResult
  Try
    '...code that might go wrong...
  Catch
    Return Content("<html><body><h1>Please Go Away</h1></body></html>", "text/html")
  End Catch
  End Try
End Function

It's just as easy to use this to return an XML string as an HTML string.

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

  • TypeScript Tops New JetBrains 'Language Promise Index'

    In its latest annual developer ecosystem report, JetBrains introduced a new "Language Promise Index" topped by Microsoft's TypeScript programming language.

Subscribe on YouTube