在 MVC 6 中读取文件

6

本文介绍了在 MVC 6 中读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想访问服务器主文件夹中的 create.sql 文件.它包含设置我的数据库的查询.我根本无法访问此文件.

I want to access my create.sql file in the main folder of my server. It contains queries to set up my database. I have a problem to access this file at all.

1) 我无法通过 Configuration 真正到达那里.我只能使用 AddJsonFileAddXmlFileAddIniFile.而且我想这不是将大 sql 文件放入其中的最佳主意.

1) I cannot really get there through Configuration. I can only use AddJsonFile, AddXmlFile, and AddIniFile. And I guess this is not the best idea to put a big sql file into any of those.

2) github 上的 Mvc 源 似乎缺少 MapPath.所以不可能使用 Server.MapPath("~/create.sql").

2) Mvc source on github seems to be missing MapPath. So no possibility of using Server.MapPath("~/create.sql").

那么如何实现呢?

推荐答案

正如评论中已经注意到和提到的,ASP.NET VNext (MVC 6) 中似乎没有 MapPath.我在这里找到了解决方法:

As already noticed and mentioned in the comments it seems that there is no MapPath in ASP.NET VNext (MVC 6). I found the workaround here:

http://forums.asp.net/t/2005166.aspx?HostingEnvironment+Equivalent+For+MapPath

基本上你需要从IApplicationEnvironment接口中获取ApplicationBasePath,该接口目前是作为服务实现的,如下解决方案:

Basically you need to get the ApplicationBasePath from IApplicationEnvironment interface, which currently is implemented as a service, following below the solution:

    private readonly IApplicationEnvironment _appEnvironment;

    public HomeController(IApplicationEnvironment appEnvironment)
    {
        _appEnvironment = appEnvironment;
    }

    public IActionResult Index()
    {
        var rootPath = _appEnvironment.ApplicationBasePath;
        return View();
    }

这篇关于在 MVC 6 中读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14