简体   繁体   中英

How i can reference static files inside my asp.net MVC core web application

I have 2 excel sheets inside my Asp.net core MVC web application under a folder named "Files" as follow:-

在此处输入图像描述

now i want to reference these files inside my TextFieldParser method as follow:-

 public async Task<IActionResult> Sync()
        {
            using (TextFieldParser parser = new TextFieldParser("*******"))

so how i can do so?

Second question, inside my startup.cs i have the following app.UseStaticFiles(); as follow:-

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {

            }
            else
            {

            app.UseStaticFiles();

so does this mean that users can access the files directly? as in my case i do not want users to have the ability to view or download the files, i only want to reference the files inside the above code. Thanks

Hope that this code will help you in some cases.

public class HomeController : Controller
{
    protected IWebHostEnvironment _host; // using Microsoft.AspNetCore.Hosting

    public HomeController(IWebHostEnvironment webHostEnvironment)
    {
        _host = webHostEnvironment;
    }

    public IActionResult Index()
    {
        string YOURCURRENTFILE = _host.ContentRootPath + @"/File/v2.csv";
        // USE YOUR TextFieldParser logic
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM