2

I call a remote action with a WebRequest like that:

var site = "http://remoteSite.com/controller/IsolateSite?url=xxx&target=in";
var request = (HttpWebRequest)WebRequest.Create(site);
var response = (HttpWebResponse)request.GetResponse();

if (response.StatusDescription == "OK")
{ /* code */ }
else
{ /* code */ }

In the remote action, I return a message or null (if id is good or not).

But response.StatusDescription is always equal to "OK" even if the action returns null.

How can I force an error as status or retrieve a message (like "Cool." or "Error.")?


The remote action sample :

public static string IsolateSite(string url, string target)
{
    var serverManager = new ServerManager();
    var isHttps = url.Contains("https");
    var regex = new Regex("^(http|https)://");
    var host = regex.Replace(url, "");
    var instance = serverManager.Sites.First(site => site.Bindings.Any(binding => binding.Host == host));
    var pool = instance.Applications[0].ApplicationPoolName;

    if ((pool.Contains("isolation") && target == "out") || (!pool.Contains("isolation") && target == "in"))
    {
        return "Error."; //or return null but the status code is OK so useless
    }

    //etc...

    return "Cool.";
}
GG.
  • 21,083
  • 14
  • 84
  • 130

2 Answers2

3

Httpstatus codes like error or cool aren't valid, here you can find all the status codes.

Instead of returning a string on error, the best you can do is throwing a HttpException:

throw new HttpException(500, "Error");

In this case I use http response status code 500 (Internal Server Error).

Another consideration is to look at ASP.NET WebApi for this kind of services, I think is suits better then an Asp.NET MVC controller.

Trevor
  • 1,561
  • 1
  • 20
  • 28
Erwin
  • 4,757
  • 3
  • 31
  • 41
0

Just take the response to a StreamReader like given below.

      var site = "http://remoteSite.com/controller/action/id";
      var request = (HttpWebRequest)WebRequest.Create(site);
      var response = (HttpWebResponse)request.GetResponse();
      StreamReader reader = new StreamReader(response.GetResponseStream());
       // This will contain the status of message e.g.    failed, ok etc.
      string resultStatus = reader.ReadToEnd(); 

The result status will give you the Error Status and Description

G . R
  • 543
  • 1
  • 6
  • 12