在asp.net mvc中引用View目录

发布于 2024-09-02 15:37:53 字数 749 浏览 2 评论 0原文

我有一些 html 文件作为常规网站的一部分,已移植到 asp.net mvc。在我的代码中,我需要读取和写入这些html文件并将它们粘贴在tinymce编辑器中

为了能够从磁盘读取和写入这个文件,过去我有一个硬编码路径,但这似乎在asp中不起作用。 net mvc 除非我做这样的事情:

写作:

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

阅读:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙女山的月亮 2024-09-09 15:37:53

如果是我,我会把这些信息存储在数据库中。这样就容易多了,从我的角度来看,磁盘空间是消耗在数据库服务器还是 Web 服务器上并不重要。另一方面,如果文件相对静态 - 即,大量读取但只有很少写入并且它们对所有用户都是通用的(站点文件而不是每个用户文件),那么您可以考虑将它们放在 Content 目录中。我经常在 Content 中为此类文件创建一个名为 static 的子目录,尽管我通常不提供从站点编辑它们的方法(可编辑内容将进入数据库)。这样你就可以构建一条通往它们的路径。

对于它的价值,您可能应该只是打开路径并阅读它们,而不是使用 WebClient。

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

注意:您可能必须创建 UrlHelper,我不认为它是控制器上的属性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文