日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

asp.net 页面静态化

發布時間:2023/12/10 asp.net 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 asp.net 页面静态化 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

?

? 頁面靜態化,有三種方式?偽靜態? 真靜態,折中法? 現在我做的是折中發

創建一個asp.net? 頁面,? 連接跳轉到還未生成的頁面

?

?

?

創建HttpHandle類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
/// <summary>
/// HttpHandle 的摘要說明
/// </summary>
public class HttpHandle : IHttpHandler
{

public bool IsReusable
{
get
{
return false;
}
}

public void ProcessRequest(HttpContext context)
{
string url = context.Request.RawUrl;
int last = url.LastIndexOf("_");
int dot = url.LastIndexOf(".");
int NewNewsID = int.Parse(url.Substring(last + 1, dot - last - 1));
string carfilepath = context.Server.MapPath("~/new/newNews_" + NewNewsID + ".html");

if (!File.Exists(carfilepath))
{
NewNews NN = new NewNews();
List<push> NewNe = NN.NewNe;
string templatepath = context.Server.MapPath("~/new/HtmlPage.html");
string templateHtml = ReadTemplate(templatepath);
templateHtml = templateHtml.Replace("{$NewNewsName}", NewNe[NewNewsID].NewNewsName);
templateHtml = templateHtml.Replace("{$NewNewsTitle}", NewNe[NewNewsID].NewNewsTitle);
templateHtml = templateHtml.Replace("{$NewNewsConters}", NewNe[NewNewsID].NewNewsConters);

WriteHtmlFile(carfilepath, templateHtml);
}
context.Response.WriteFile(carfilepath);

}


// private string ReadTemplate(string templatePath)
//{
// //檢測模板文件是否存在
// if (!File.Exists(templatePath))
// {
// //模板文件不存在,拋出異常
// throw new Exception("汽車詳情頁面的模板文件未找到!");
// }
// //創建文件流
// FileStream fs = new FileStream(templatePath, FileMode.Open);
// //創建流讀取器
// StreamReader sr = new StreamReader(fs);
// //讀取文件流中的文本
// string templeteHtml = sr.ReadToEnd();
// //關閉流讀取器
// sr.Close();
// //關閉文件流
// fs.Close();
// //返回讀取的模板HTML
// return templeteHtml;
//}
寫靜態文件
//private void WriteHtmlFile(string savedPath, string htmlStr)
//{
// FileStream fs = new FileStream(savedPath, FileMode.Create);
// StreamWriter sw = new StreamWriter(fs);
// sw.Write(htmlStr);
// sw.Close();
// fs.Close();
//}

//讀取模板方法


private string ReadTemplate(string templatePath)
{
if (!File.Exists(templatePath))
{
throw new Exception("報錯");
}
FileStream fs = new FileStream(templatePath, FileMode.Open);
StreamReader sr = new StreamReader(fs);
string templeteHtml = sr.ReadToEnd();
sr.Close();
fs.Close();
return templeteHtml;
}

//
private void WriteHtmlFile(string savedPath, string htmlStr)
{
FileStream fs = new FileStream(savedPath, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(htmlStr);
sw.Close();
fs.Close();
}
}

?

?

創建一個

?

NewNews? 表示服務器

添加數據

?

public List<push> NewNe = new List<push>();
public NewNews()
{
NewNe.Add(new push() { NewNewsID = 0, NewNewsName = "今天是2018/5/30", NewNewsTitle = "今天下雨了", NewNewsConters = "圓周" });
NewNe.Add(new push() { NewNewsID = 1, NewNewsName = "今天下雨了嘛是", NewNewsTitle = "今天下雨了", NewNewsConters = "32" });
NewNe.Add(new push() { NewNewsID = 2, NewNewsName = "今天是2018/5/30", NewNewsTitle = "今天下雨了", NewNewsConters = "煩煩煩煩煩煩22342" });
NewNe.Add(new push() { NewNewsID = 3, NewNewsName = "今天是2018/5/30", NewNewsTitle = "今天下雨了", NewNewsConters = "2342" });

}
}

?

創建一個push?類? 添加屬性字段

/// <summary>
/// push 的摘要說明
/// </summary>
public class push
{
public int NewNewsID { get; set; }
public string NewNewsName { get; set; }
public string NewNewsTitle { get; set; }
public string NewNewsConters { get; set; }
}

?

?

?

新建一個html頁面? .為模板

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>表格</title>
</head>
<body>

<div>
<p>{$NewNewsName}</p>
<p>{$NewNewsConters}</p>
<p>{$NewNewsTitle}</p>

</div>

</body>
</html>

?

這樣子?就可以了

轉載于:https://www.cnblogs.com/whatarey/p/9112493.html

總結

以上是生活随笔為你收集整理的asp.net 页面静态化的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。