如何在ASP.Net MVC中读取文件的内容? [英] How can I read the contents of a file in ASP.Net MVC?

查看:484
本文介绍了如何在ASP.Net MVC中读取文件的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.Net MVC控制器操作,该操作需要返回包含另一个html文件内容的j​​son结果(实际上是jsonp,但这并不重要).所以基本上,我想这样做:

I have an ASP.Net MVC controller action that needs to return a json result (it's actually jsonp, but that's not important) that contains the contents of another html file. So basically, I want to do this:

[JsonpFilter]
public JsonResult GetHeader()
{
    var html = System.IO.File.ReadAllText("/htm/external/header.htm");

    return Json(new { html = html }, JsonRequestBehavior.AllowGet);
}

但是,找不到正确的文件.当我希望它查看服务器的根目录时(当然加上/htm/external),它正在C目录中查找(找不到路径'C:\ htm \ external \ header.htm'的一部分).

However, it's not finding the right file. It's looking in the C directory (Could not find a part of the path 'C:\htm\external\header.htm'), when I want it to look at the server's root (plus /htm/external, of course).

如何读取此文件?它在外部可用,因此我想我可以对其进行单独的Web请求,但看来我应该可以直接将其定位为目标.

How can I read this file in? It's externally available, so I guess I could make a separate web request for it, but it seems like I should be able to target it directly.

推荐答案

您可以使用

这篇关于如何在ASP.Net MVC中读取文件的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆