ASP.NET: Store user information on submit
i'm kinda new into asp.net so i need some help.
i have this form, that the data is stored in a SQL db using Linq.
my question is, how can i a开发者_如何转开发dd a column, that will have all the information about the sender, meaning IP address, browser, referrer etc.
the thing is is i want to store it in one field.
i come from a php knowledge that has been long forgotten by me, but i still remember there was some serialize command, that you could run on an array and store it in a db, then when you want it back to an array you would just run deserialize command and it would go back to an array.
so what you guys think the best way of preforming this action is?
Thanks in advance!
Assemble the data in any serialized form yourself and save it to the DB column. One way could be to use xml format
var sb = new StringBuilder();
sb.Append("<userdata>");
sb.Append("<browser>").Append(Request.Browser.Browser).Append("</browser>");
sb.Append("<ipaddress>").Append(Request.UserHostAddress).Append("</ipaddress>");
sb.Append("<referrer>").Append(Request.UrlReferrer.AbsoluteUri).Append("</referrer>");
sb.Append("</userdata>");
string SenderData = sb.ToString();
// Save the SenderData string to a varchar DB column.
// The above code only proposes the serialization idea.
// As commented, it doesn't encode the user values.
// One of the way to take care of that could be using XmlTextWriter.
var sb = new StringBuilder();
var xtw = new XmlTextWriter(new StringWriter(sb));
xtw.WriteStartElement("userdata");
xtw.WriteElementString("browser", Request.Browser.Browser);
xtw.WriteElementString("ipaddress", Request.UserHostAddress);
xtw.WriteElementString("referrer", Request.UrlReferrer.AbsoluteUri);
xtw.WriteEndElement();
xtw.Close();
string SenderData = sb.ToString();
// Save the SenderData string to a varchar DB column.
// Another suggested way
public class UserData
{
public string Browser { get; set; }
public string IPAddress { get; set; }
public string Referrer { get; set; }
public UserData()
{
}
public UserData(string browser, string iPAddress, string referrer)
{
Browser = browser;
IPAddress = iPAddress;
Referrer = referrer;
}
}
var userData = new UserData(Request.Browser.Browser, Request.UserHostAddress, Request.UrlReferrer.AbsoluteUri);
var xmls = new System.Xml.Serialization.XmlSerializer(typeof(UserData));
var sb = new StringBuilder();
var xtw = new XmlTextWriter(new StringWriter(sb));
xmls.Serialize(xtw, userData);
xtw.Close();
string SenderData = sb.ToString();
// Save the SenderData string to a varchar DB column.
精彩评论