0

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 Answer 1

1

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.

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

6 Comments

@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
@tvanfosson - what data type field would you use in SQL server to store this ??
@ooo -- that depends on the expected size. I've done both varchar(x000) and varchar(max).
or you can create a constraint. take a look this. blog.sqlauthority.com/2007/06/01/…
someone else suggested ntext.. what is the difference?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.