cmp cmp - 11 days ago 5
ASP.NET (C#) Question

Using Request to Response.Write an error message

I wish to use output an error message to the page (using

Response.Write
) depending on an optional paramter of
Request
.

string errorMessage = "";

if (!string.IsNullOrEmpty(errorMessage))
{
errorMessage = Request["msg"].Trim();
}

switch (errorMessage)
{
case "error":
Response.Write(@"We apologise for the delay, this is due to a site issue. We expect this to be resolved shortly. Please <a href=""/"">try again</a> in a few minutes.");
break;
case "288":
Response.Write("We will be back soon!");
break;
default:
Response.Write("We are working on it...");
break;
}


I don't get an exception, and the default switch works, it prints: "We are working on it..." to the page, but it doesn't change depending on the parameter "msg" passed to the request.

Answer

Your if-statement will always return false so you never read from the Request.

If you want to check whether a parameter named "key" was passed, you can check Request["msg"] instead of errorMessage, because MSDN says:

If the specified key is not found, then null is returned.

string errorMessage = "";

if (!string.IsNullOrEmpty(Request["msg"])) // 
{
    errorMessage = Request["msg"].Trim();
}

switch (errorMessage)
{
  /*...*/
}
Comments