2015-06-04 13 views
8

Tôi muốn truy cập tệp create.sql của mình trong thư mục chính của máy chủ của tôi. Nó chứa các truy vấn để thiết lập cơ sở dữ liệu của tôi. Tôi gặp sự cố khi truy cập tệp này.Đọc một tệp trong MVC 6

1) Tôi không thể thực sự đến đó thông qua Configuration. Tôi chỉ có thể sử dụng AddJsonFile, AddXmlFileAddIniFile. Và tôi đoán đây không phải là ý tưởng tốt nhất để đặt một tệp sql lớn vào bất kỳ tệp nào trong số đó.

2) Mvc source on github dường như bị thiếu MapPath. Vì vậy, không có khả năng sử dụng Server.MapPath("~/create.sql").

Làm cách nào để đạt được điều này?

+0

'MapPath' là một phần của ASP.NET. –

+3

Bạn có thể sử dụng thuộc tính ApplicationBasePath trên dịch vụ IApplicationEnvironment để lấy đường dẫn gốc của ứng dụng của bạn ... tò mò, kịch bản mà bạn đang cố gắng đạt được là gì? –

+0

@KiranChalla Nó hoạt động, tuyệt vời, cảm ơn bạn! Nếu bạn muốn, xin vui lòng viết nó như là một câu trả lời và tôi sẽ chấp nhận nó :) Và liên quan đến câu hỏi của bạn - Tôi muốn xây dựng cấu trúc cơ sở dữ liệu của tôi (bảng, chức năng, vv) trên một máy chủ khởi động. –

Trả lời

15

Như đã được chú ý và đề cập trong nhận xét có vẻ như không có MapPath trong ASP.NET VNext (MVC 6). Tôi tìm thấy cách giải quyết ở đây:

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

Về cơ bản bạn cần phải nhận được ApplicationBasePath từ giao diện IApplicationEnvironment, hiện đang được thực hiện như một dịch vụ, sau đây là giải pháp:

private readonly IApplicationEnvironment _appEnvironment; 

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

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

Và cũng có thể, thay vì của tiêm IApplicationEnvironment bạn có thể sử dụng PlatformServices.Default.Application.ApplicationBasePath.

EDIT: Đây là một thực thể của MapPath/UnmapPath như phần mở rộng để PlatformServices:

removed (see EDIT2) 

EDIT2: Hơi sửa đổi, IsPathMapped() thêm cũng như một số kiểm tra để xem nếu bản đồ path/unmapping là thực sự cần thiết.

public static class PlatformServicesExtensions 
{ 
    public static string MapPath(this PlatformServices services, string path) 
    { 
     var result = path ?? string.Empty; 
     if (services.IsPathMapped(path) == false) 
     { 
      var wwwroot = services.WwwRoot(); 
      if (result.StartsWith("~", StringComparison.Ordinal)) 
      { 
       result = result.Substring(1); 
      } 
      if (result.StartsWith("/", StringComparison.Ordinal)) 
      { 
       result = result.Substring(1); 
      } 
      result = Path.Combine(wwwroot, result.Replace('/', '\\')); 
     } 

     return result; 
    } 

    public static string UnmapPath(this PlatformServices services, string path) 
    { 
     var result = path ?? string.Empty; 
     if (services.IsPathMapped(path)) 
     { 
      var wwwroot = services.WwwRoot(); 
      result = result.Remove(0, wwwroot.Length); 
      result = result.Replace('\\', '/'); 

      var prefix = (result.StartsWith("/", StringComparison.Ordinal) ? "~" : "~/"); 
      result = prefix + result; 
     } 

     return result; 
    } 

    public static bool IsPathMapped(this PlatformServices services, string path) 
    { 
     var result = path ?? string.Empty; 
     return result.StartsWith(services.Application.ApplicationBasePath, 
      StringComparison.Ordinal); 
    } 

    public static string WwwRoot(this PlatformServices services) 
    { 
     // todo: take it from project.json!!! 
     var result = Path.Combine(services.Application.ApplicationBasePath, "wwwroot"); 
     return result; 
    } 
} 

EDIT3:PlatformServices.WwwRoot() trở lại con đường thực hiện thực tế và trong lõi .net 2.0, chế độ DEBUG nó là xxx \ bin \ Debug \ netcoreapp2.0, trong đó, rõ ràng không phải là những gì được yêu cầu. Thay vào đó, hãy thay thế PlatformServices bằng IHostingEnvironment và sử dụng environment.WebRootPath.