Reading ASPX files from the file system and rendering in HTML

Can I read a file aspxand display it as a file htmland write the resulting file htmlto disk?

The file .aspxis on a file system without a codebehind file. If possible, provide an example code.

+3
source share
6 answers

from remote URL

byte[] buf = new byte[8192];
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(path);
webRequest.KeepAlive = false;
string content = string.Empty;
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
if (!(webResponse.StatusCode == HttpStatusCode.OK))
    if (_log.IsErrorEnabled) _log.Error(string.Format("Url {0} not found", path));

Stream resStream = webResponse.GetResponseStream();

int count = 0;
do
{
    count = resStream.Read(buf, 0, buf.Length);
    if (count != 0)
    {
        content += encoding.GetString(buf, 0, count);
    }
}
while (count > 0);

from a network or virtual path

string content = string.Empty;
path = HttpContext.Current.Server.MapPath(path);

if (!File.Exists(path))
if (_log.IsErrorEnabled) _log.Error(string.Format("file {0} not found", path));

StreamReader sr = new StreamReader(path, encoding);
content = sr.ReadToEnd();
+2
source

This is what ASP.NET does all the time. It searches for an ASPX page in the file system, compiles it if necessary, and then processes the request.

Codebehind is optional. You can have a site with only ASPX, without pre-compiled code.

ASPX codebehind

<%@ Page language="c#" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
    <title>ClearCache</title>
</HEAD>
<body>
    <form id="ClearCache" method="post" runat="server">
        <% 
        IList keys = new ArrayList(Cache.Count);
        foreach (DictionaryEntry de in Cache) 
            keys.Add(de.Key);

        foreach (string key in keys)
        {
            this.Response.Write(key + "<br>");
            Cache.Remove(key);

        }

    %>
    </form>
</body>
</HTML>

html:

var wc = new WebClient();
wc.DownloadFile(myUrl, filename);

- ASP.NET, . Cassini . :

var server = new Server(80,"/", pathToWebSite);
server.Start();
var wc = new WebClient();
wc.DownloadFile(server.RootUrl + "myPage.aspx", filename);
server.Stop();

, .

, RuntimeHost, 4. - . , .

+1

ASPX = > HTML .
codebehind, .

Mono Project . , .

, , aspx xml ( ) .

0

, , , ASP.NET. ASP.NET HTML ASPX , IHttpModule, .

0

If I understand your question correctly, do you need an instance of the created page class (i.e. the aspx page is compiled) and, in the end, the resulting html? But do you want this to happen outside the context of the web server request?

If you are looking for html after processing an aspx page, why not just grab the html returned after the page is actually rendered through IIS or something else?

Perhaps if you share your motivation (attempts) for this, you will get some solid suggestions ...

0
source

Source: https://habr.com/ru/post/1752451/


All Articles