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.";
}
3
  • How does a sample action look like where you want to return an error case? Commented Aug 30, 2012 at 9:53
  • what is your response.StatusCode? Commented Aug 30, 2012 at 10:23
  • @Raj - OK (System.Net.HttpStatusCode) Commented Aug 30, 2012 at 11:51

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.