Solved

how do we read .htm file on my web directory and display the contents

Posted on 2013-01-16
8
174 Views
Last Modified: 2013-01-25
string temp = File.ReadAllText(Server.MapPath("http://localhost:50647/Prod1/home.htm"));

The above is not working.  Is there a better way?

I want to display the entire htm file as

Response.Write(temp);
0
Comment
Question by:goodk
  • 4
  • 4
8 Comments
 
LVL 18

Assisted Solution

by:Rajar Ahmed
Rajar Ahmed earned 500 total points
ID: 38783140
http://www.dotnetspider.com/resources/22019-read-html-file-text-file-ASP-NET.aspx
string file = Server.MapPath("~/abc.htm");
        StreamReader sr;
        FileInfo fi = new FileInfo(file);
        string input = "<pre>";
        if (File.Exists(file))
        {
            sr = File.OpenText(file);
            input += Server.HtmlEncode(sr.ReadToEnd());
            sr.Close();
        }
        else { 
         input="notExis";
        }
        input += "</pre>";
        Label1.Text = input;
 [subtitle][/subtitle]

Open in new window

0
 

Author Comment

by:goodk
ID: 38783277
I have the following but it is not working - what else I need to add?

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Web.Caching;
using System.Web.SessionState;
using System.Web.Profile;
using System.Reflection;
using System.Web.Administration;
using System.Data.Common;
using System.Data.OleDb;
using System.Globalization;
using System.Collections;
0
 
LVL 18

Assisted Solution

by:Rajar Ahmed
Rajar Ahmed earned 500 total points
ID: 38783324
Your missing System.IO;
Complete code,

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="readhtmlInCodebehind.aspx.cs" Inherits="readhtmlInCodebehind" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>

Open in new window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class readhtmlInCodebehind : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string file = Server.MapPath("~/abc.htm");
        StreamReader sr;
        FileInfo fi = new FileInfo(file);
        string input = "<pre>";
        if (File.Exists(file))
        {
            sr = File.OpenText(file);
            input += Server.HtmlEncode(sr.ReadToEnd());
            sr.Close();
        }
        else { 
         input="notExis";
        }
        input += "</pre>";
        Label1.Text = input;
    }
}

Open in new window

0
 

Author Comment

by:goodk
ID: 38783488
Error      1      The name 'Server' does not exist in the current context
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 18

Assisted Solution

by:Rajar Ahmed
Rajar Ahmed earned 500 total points
ID: 38783503
string file = HttpContext.Current.Server.MapPath("~/abc.htm");

Open in new window

0
 

Accepted Solution

by:
goodk earned 0 total points
ID: 38784666
Oops, but the html file is showing up instead of content
0
 
LVL 18

Assisted Solution

by:Rajar Ahmed
Rajar Ahmed earned 500 total points
ID: 38798811
Change to this on your current code,
input += Server.HtmlDecode(sr.ReadToEnd());

Open in new window


Another Option,
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="readhtmlInCodebehind.aspx.cs" Inherits="readhtmlInCodebehind" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <%
        Response.WriteFile("~/abc.htm");
         %>
    </div>
    </form>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:goodk
ID: 38818009
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now