Solved

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

Posted on 2013-01-16
8
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:goodk
ID: 38783488
Error      1      The name 'Server' does not exist in the current context
0
 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…

740 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