Click here to Skip to main content
15,891,745 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

How to display error or any message in html error template from aspx page in c# asp.net?

i have tried this code for display message but my message its not displaying.

My code:-
C#
protected void Button1_Click(object sender, EventArgs e)
   {
       PopulateBody("Exception in this page");
       Response.Redirect("ErrorPage.htm");
   }

   private string PopulateBody(string ErrorMsg)
   {
       string body = string.Empty;
       using (StreamReader reader = new StreamReader(Server.MapPath("~/ErrorPage.htm")))
       {
           body = reader.ReadToEnd();
       }
       body = body.Replace("{MESSAGE}", ErrorMsg);
       return body;
   }


Text its not displaying in Error html page.
How can we display error or any message in html template.

Please help me.

Thanks in Advance.

Ankit Agarwal
Software Engineer
Posted
Comments
F-ES Sitecore 25-Nov-15 5:31am    
Google how to implement a custom 500 error page. The problem with your method (when fixed) is that the browser receives a 200 OK response so doesn't know the action generated an error. This can affect SEO and is a general breach of internet standards.

Instead just throw your exceptions so that they are caught by the custom 500 page and the browser will receive a problem error response too.
Agarwal1984 25-Nov-15 5:36am    
I don't have use custom error page.
I have to use own error page, and i have write own message on every catch block.

1 solution

You can try
C#
protected void Button1_Click(object sender, EventArgs e)
   {
       string str = PopulateBody("Exception in this page");
       this.Page.Controls.Clear();
       Response.Write(str);
   }
 
   private string PopulateBody(string ErrorMsg)
   {
       string body = string.Empty;
       using (StreamReader reader = new StreamReader(Server.MapPath("~/ErrorPage.htm")))
       {
           body = reader.ReadToEnd();
       }
       body = body.Replace("{MESSAGE}", ErrorMsg);
       return body;
   }
 
Share this answer
 
Comments
Agarwal1984 25-Nov-15 5:31am    
I used this code:-

protected void Button1_Click(object sender, EventArgs e)
{
string str = PopulateBody("Exception in this page");
this.Page.Controls.Clear();
Response.Write(str);
Response.Redirect("ErrorPage.htm");
}

private string PopulateBody(string ErrorMsg)
{
string body = string.Empty;
using (StreamReader reader = new StreamReader(Server.MapPath("~/ErrorPage.htm")))
{
body = reader.ReadToEnd();
}
body = body.Replace("{MESSAGE}", ErrorMsg);
return body;
}

but message its not displaying.
Agarwal1984 25-Nov-15 5:32am    
<body>
<div class="wrap">
<div class="main">

ABC

{MESSAGE}

<p>There's a lot of reasons why this page is<span class="error"></span>.<br>
<span>Don't waste your time enjoying the look of it</span></p>
<div class="search">
<form>
<!--<input type="text" >-->
<input type="submit" value="Submit">
</form>
</div>

</div>
</body>

its my ErrorMessage Template
Agarwal1984 25-Nov-15 5:44am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900