4

I have a asp.net MVC razor C# application which has 1 controller and 1 POST function which accepts a parameter. And the function returns a HttpResponseMessage.

public class VersionController : Controller
{
    [HttpPost]
    public HttpResponseMessage LatestClientVersion(string myVar)
    {
        string outputMessage = "This is my output";
        ...

        var resp = new HttpResponseMessage(HttpStatusCode.OK);
        resp.Content = new StringContent(outputMessage, Encoding.UTF8, "text/plain");
        return resp;
    }
}

For testing purposes I've used Postman to do a POST request to the URL. It responds with:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content:      System.Net.Http.StringContent, Headers:
{
    Content-Type: text/plain; charset=utf-8
}

My statuscode reponds well, but I don't see my string "This is my output"

So I thought that maybe this is something to do with C# specific stuff, so I made a C# winforms application to test. So when I click the button it does the following:

    public void TestWebRequest()
    {
        try
        {               
            WebRequest request = WebRequest.Create(txtURL.Text);
            request.Method = "POST";
            string postData = "myVar=test";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            WebResponse response = request.GetResponse();
            HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();

            dataStream = response.GetResponseStream();

            StreamReader reader = new StreamReader(dataStream);

            string responseFromServer = reader.ReadToEnd();

            // Send text to the textbox
            SetOutputText(responseFromServer);

            reader.Close();
            dataStream.Close();
            response.Close();
        }
        catch (Exception ex)
        {
            SetOutputText(ex.Message);
        }            
    }
 }

This function works perfect but I still get the same reponse like the one in Postman... How can I get the actual content "This is my output"?

Edit

I've made another simple HttpGet request

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Mvc;
using System.Web.Configuration;
using System.Net.Http.Headers;
using System.Net;
using System.Text;

namespace MyWebService.Controllers
{
    public class VersionController : Controller
    {
         [HttpGet]
         public HttpResponseMessage Test()
        {
            HttpResponseMessage response = new HttpResponseMessage();
            response.Content = new StringContent("This is my output");
            return response;
        }
     }
 }

While using Postman I get following result

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content:     System.Net.Http.StringContent, Headers:
{
    Content-Type: text/plain; charset=utf-8
}
koala
  • 1,544
  • 1
  • 20
  • 33
  • If PostMan doesn't show it, HttpWebResponse won't either. Make sure you don't set `outputMessage` to null or empty. – CodeCaster Mar 30 '15 at 11:18
  • I can assure that the outputMessage is not null or empty when it reaches: resp.Content = new StringContent(outputMessage, Encoding.UTF8, "text/plain"); – koala Mar 30 '15 at 11:54
  • Sure, but your PostMan output shows `Content: System.Net.Http.StringContent`, which is the _type name_ of the object you're trying to return. Again, make sure you don't overwrite `outputMessage`. The code you show should do what you expect it to, there's something else you're not showing. – CodeCaster Mar 30 '15 at 12:06
  • I've edited my post with a simple HttpGet method and this one gives the same response, I must be missing something here... – koala Mar 30 '15 at 12:14

1 Answers1

5

You're mixing up WebAPI and MVC.

For WebAPI, the HttpResponseMessage (with Content = new StringContent("the string")) would work.

For MVC, the syntax to return a string is (note the ActionResult return type and Content() call):

public ActionResult Test() 
{
    return Content("This is my output");
}
Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • I've tested with an API controller and it works indeed! Didn't know that this was a difference between MVC en API controller – koala Mar 30 '15 at 12:25
  • @koala in ASP.NET 5 / MVC 6 / vNext they're attempting to unify the experience. :) Differences like this should be eliminated in the future. – CodeCaster Mar 30 '15 at 12:27