views:

14

answers:

1

i have some html files as part of a regular website that has been ported over to asp.net mvc. In my code i need to read and write these html files and stick them in a tinymce editor

To be able to read and write this file from disk in the past i had a hard coded path but this doesn't seem to work in asp.net mvc unless i do something like this:

Writing:

    string _urlDirectory = @"c:\hosting\MySite\Views\Members\newsletters\test.html";
    System.IO.File.WriteAllText(_urlDirectory, htmlData_);

Reading:

        string url = @"c:\hosting\MySite\Views\Members\newsletters\test.html";
        var req = WebRequest.Create(url);
        var response = req.GetResponse();
        StreamReader sr = new StreamReader(response.GetResponseStream());
        string htmlData_ = sr.ReadToEnd();

i am moving my site from one data center to another and the directory structure is changing. instead of just changing the hard coded path to another hard coded path i wanted to see if there was a more relative way to reference these files.

+1  A: 

If it were me, I would store the information in a database. It'e much easier that way and, from my perspective, it little matters whether the disk space is consumed on the database server or the web server. If, on the other hand, the files are relatively static -- i.e., lots of reads but only few writes and they are common to all users (site files not per-user files), then you could consider putting them in the Content directory. I often create a subdirectory in Content named static for just such files, though I don't usually provide a way to edit them from the site (editable content would go in the DB). This way you can construct a path to them.

For what it's worth, you should probably just open the path and read them, rather than using a WebClient.

    string url = Url.Content( "~/content/newsletters/test.html" );
    string path = Server.MapPath( url );
    StreamReader sr = new StreamReader(path);
    string htmlData_ = sr.ReadToEnd();

Note: you may have to create the UrlHelper, I don't think its a property on the Controller.

tvanfosson
@tvanfosson - thanks for your feedback. I agree that moving these files into a db is the way to go. that will make life much simpler
ooo
@tvanfosson - what data type field would you use in SQL server to store this ??
ooo
@ooo -- that depends on the expected size. I've done both varchar(x000) and varchar(max).
tvanfosson
or you can create a constraint. take a look this. http://blog.sqlauthority.com/2007/06/01/sql-server-2005-constraint-on-varcharmax-field-to-limit-it-certain-length/
cem
someone else suggested ntext.. what is the difference?
ooo
@ooo - if you need unicode characters you should use nvarchar(max). ntext is a similar, but deprecated data type. See here: http://msdn.microsoft.com/en-us/library/ms187993.aspx. I wouldn't think you would have actual unicode characters in your html, but rather encodings that translate to unicode characters when they are rendered. I don't know how TinyMCE handles unicode input, though.
tvanfosson