1

How to build a page where visitors can add comments to the page? I know it's possible with a db, the question is - can I do it without a db? Perhaps by saving a new element to the existing .aspx file in the correct location (after the last comment inserted), how do I do it?

Any other elegant idea how to implement this?

The reason I don't use a db in this case is for simplicity and for fast loading of the page - am I right with my approach?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
BornToCode
  • 9,495
  • 9
  • 66
  • 83
  • 2
    You'll find that simplicity and performance are a lot easier to achieve with simply using a database than with trying to modify a static HTML file in memory. This approach, in the same of simplicity, will be a lot more difficult and a lot less stable. – David Jun 07 '12 at 11:00
  • 1
    You do with streamwriter by insert comments in text file – Nikhil D Jun 07 '12 at 11:01
  • 1
    All I/Os will cost almost same, file or DB. However it would be easy for you to maintain on Database. What else do you have in mind? files, Distributed Cache, Key/Value memory? – Adil Jun 07 '12 at 11:04

5 Answers5

1

No!

You need to store your data somewhere and query it latter on. How do you expect to do it? You definitively need somewhere to store that information and a db is the best option.

Diego
  • 34,802
  • 21
  • 91
  • 134
  • 1
    The option he is looking for most likely contains a form at the bottom of the page and when you hit submit a streamwrite takes that data and writes it to the bottom of the HTML page. It's essentially a trade off from not having to make a db connection and having more markup and a longer page load. The without a db way may be "more efficient" if there are only a few comments but it would certainly be easier and better to store the data in a db for large amounts. It would also become easier to format. – Tony318 Jun 07 '12 at 11:13
  • @Tony318 - You are the only one who understood me! I guess I wasn't clear enough.. 1.my problem is how do I find in the html file the correct location to update (I'd like to add the new div or li not to the bottom of the page but somewhere in the middle right after the last comment added..?). 2.Why in "large amounts a db is better" I can't see any difference? loading static html page would always be better, am I wrong? – BornToCode Jun 07 '12 at 11:20
  • 1
    @BornToCode: Loading a static file would likely be faster, yes. But as the file grows, _writes_ to the file will get slower and slower. Also, in order to find the correct place to write to the file, you're going to have to parse the file. That's also comparatively slow. The industry as a whole has put a _lot_ of time and effort optimizing relational databases. No offense, but I doubt you're going to beat that. Aside from performance, your approach will result in more code, more manual work to tweak it to be "just right," and other problems. (Concurrency, recovery from bad data, etc.) – David Jun 07 '12 at 11:40
1

I also suggest the use of a database, how ever some time we need to make something low cost, simple and fast for a small web site with not so many traffic.

So this is what I suggest you. Appends your commends to a file with File.AppendText, and then include the file using this command, on the place you like to see this comments.

<!--#include file="CommentsA.htm"--> 

Do not append your comments in the aspx file because this can cause recompile of your site.

As I say again, this is a simple idea, for something fast and low cost. You need here to take care the attacks via javascript injection and the spam, nether you have the luxury to approve/reject messages.

This is for sure the faster and easiest way - but you do not have control over the comments.

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • why appending the comments can cause recompile of the site, is it because the server sees that the file was modified and then it recompiles? How do I take care of javascript injection? – BornToCode Jun 07 '12 at 11:40
  • @BornToCode You make the comments Server.HtmlEncode, (not your format tags, only what the user writes on it) – Aristos Jun 07 '12 at 11:42
  • @BornToCode this way is not cause any recompile !, only if you append on aspx files. – Aristos Jun 07 '12 at 11:43
  • @BornToCode: The ASP.NET engine will probably interpret the ASPX file changes as cause to re-compile, yes. This will dramatically impact performance if the site has to re-JIT after every write (on the next read). As for injection, do some research into "cross-site scripting." You don't want people to be able to write code on your site. Another thought... How are you going to update your code without overwriting your data? Separating the data into a different data store solves this. That data store doesn't _have_ to be a database. You could also store data in, say, and XML file. – David Jun 07 '12 at 11:44
  • 1
    @David yes of course the xml file, or other file-database like xml can be used, I just throw an idea here. The aspx is not recompile the site if you use this include method ! The append is done on the commants.htm file, not on aspx ! – Aristos Jun 07 '12 at 11:48
  • @David I see from this link http://stackoverflow.com/questions/53728/will-html-encoding-prevent-all-kinds-of-xss-attacks that preventing cross-site scripting is quite a pain, will just filtering all comments containing the word 'script' will do the job? – BornToCode Jun 07 '12 at 12:35
  • 1
    @BornToCode I told you, only make the HtmlEncode on the user input, when you write them to the file. The script is not enough, the symbol `<` the most important, but stick to the HtmlEncode – Aristos Jun 07 '12 at 12:37
  • 1
    @BornToCode: Listen to Aristos on this one. It can certainly be quite a pain, but it's worth gaining the knowledge and understanding. Remember that there is no single thing you can do to make your application secure, it's always an ongoing learning process. Never assume your application is secure. – David Jun 07 '12 at 12:42
1

can I do it without a db?

You can steer your car with your feet, that doesn't necessarily make it a good idea. There are so many benefits to using a database such as security, concurrency and indeed performance. You should look at this as an opportunity to pick the right tool for the job rather than reinventing the wheel.

Mr Wilde
  • 649
  • 8
  • 19
0

The asp.net site has a great set of tutorials from moving from static pages into data driven pages:

You may wish to go down the route that microsoft describtes as "web pages", which is the easier but more basic version of it's bigger brothers web-forms and mvc-framework.

Here is a direct link to the data tutorials for "web pages": http://www.asp.net/web-pages/tutorials/data

Looking at these tutorials will help you descide which is best for you.

But you may wish to look at:

http://www.asp.net/web-pages

http://www.asp.net/web-forms

http://www.asp.net/mvc

Alex KeySmith
  • 16,657
  • 11
  • 74
  • 152
0

I still suggest using a database but this is basically the thought process i have of doing it your way. I have never done this so it is just going to be theory not code and someone may need to add to it.

Create a div where you want your comments on your aspx page, you can name this div "comments" or something. Only comments go in here though. What you are going to do is make a generic form probably having a text area for the comments, and a submit button. When the submit button is pressed it triggers an event that uses a streamwriter to directly write to the text file. You need to be able to loop through the lines of the text file until you hit your div and then it will write the html to the text file and save it. This means when you use your streamwriter you are going to have all the code laid out for the formatting of the comment already there and are just grabbing the user name and text of the comment.

Tony318
  • 552
  • 2
  • 9