Tags » Serverside

MVC4 ASPNET async action methods on net452

Recently I shifted from working with .net 451 to .net 452 and noticed some changes that I thought you might have stumbled upon as well. This problem deals specifically with… 254 more words

Programming

Rails : how to handle : some field's presence is mandory in some context but not always

You might stuble upon some situation like “how to handle : some field’s presence is mandory in some context but not always”. It is how I decided to handle such situation 134 more words

Ruby On Rails

IF statements, switches and conditional assignments

Different ways to write conditional statements:


// IF

if(condition)

{

//Do something

}

// If ELSE

if(condition)

{

// Do something

}

else

{

// if false, do something else

}

// IF, ELSE IF

if(condition1)

{

// Do something

}

else if(condition2)

{

// If not condition 1 and condition 2, do something

}

// Note that you can omit the brackets if you only need to do one action. 110 more words

How to check if a resource is available on server

Sometimes it can be useful to know if a resource is available before trying to load it from another server.

See code below:


/// <summary>
/// This method will check a url to see that it does not return server or protocol errors
/// </summary>
/// <param name="url">The path to check</param>
/// <returns></returns>
public static bool UrlIsValid(string url)
{
try
{
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Timeout = 5000; //set the timeout to 5 seconds to keep the user from waiting too long for the page to load
request.Method = "HEAD"; //Get only the header information -- no need to download any content

int statusCode = Null.NullInteger; // NullInteger is -1
using (var response = request.GetResponse() as HttpWebResponse)
{
statusCode = Convert.ToInt32(response.StatusCode);
}

// Check Status Code
if (statusCode >= 100 && statusCode < 400) //Good requests
return true;
else if (statusCode >= 500 && statusCode <= 510) //Server Errors
return false;
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError) //400 errors
{
return false;
}
else
{
_logger.Error(String.Format("Unhandled status [{0}] returned for url: {1}", ex.Status, url), ex);
}
}
catch (Exception ex)
{
_logger.Error(String.Format("Could not test url {0}.", url), ex);
}
return false;
}