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?

3
  • 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. Commented Jun 7, 2012 at 11:00
  • 1
    You do with streamwriter by insert comments in text file Commented Jun 7, 2012 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? Commented Jun 7, 2012 at 11:04

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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 You make the comments Server.HtmlEncode, (not your format tags, only what the user writes on it)
@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 !
@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
@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.
|
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.

3 Comments

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 - 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: 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.)
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.

Comments

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.