开发者

Referencing View directory in asp.net mvc

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 a开发者_开发百科ble 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜