[英]ASP.NET C# MVC Best way to store lots of information/value without using database
我正在尋找一種解決問題的方法(這里是新手),因此基本上我正在創建一個頁面,用戶可以輸入其計算機規格並提交以生成PDF報告(基本上包含帶有我們徽標的計算機規格)。 事實是,我不想將用戶輸入的值/數據存儲在數據庫中。 還有其他方法嗎? 我當時想直接生成PDF文件並將其存儲在我們的硬盤中,下次他們可以直接在其中訪問它時,從長遠來看,它似乎很昂貴。
謝謝你的幫助
您可以使用XML序列化將數據存儲在XML文件中。
檢查以下pseudo
代碼:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace XmlMvcApplication1.Controllers
{
[Serializable]
public class TestData
{
public string id { get; set; }
public string Name { get; set; }
}
public class HomeController : Controller
{
public ActionResult SaveItems(string id, string name)
{
TestData obj = new TestData();
obj.id = id;
obj.Name = name;
string file = Server.MapPath("~/Data/") + "\\" + id + ".xml";
if (System.IO.File.Exists(file))
System.IO.File.Delete(file);
FileStream fs = new FileStream(file, FileMode.CreateNew);
//store into xml file
XmlSerializer x = new XmlSerializer(typeof(TestData));
x.Serialize(fs, obj);
fs.Flush();
fs.Close();
return View();
}
public ActionResult GetItems(string id)
{
string file = Server.MapPath("~/Data/") + "\\" + id + ".xml";
if (System.IO.File.Exists(file))
{
XDocument xml = XDocument.Load(file);
var xmlSerializer = new XmlSerializer(typeof(TestData));
var nodes = xml.Descendants("TestData")
.Select(rr => xmlSerializer.Deserialize(rr.CreateReader()) as TestData);
var deSerializedData = nodes.First();
//YOUR CODE HERE
}
return View();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.