John John
John John

Reputation: 1

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

enter image description here

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

Upvotes: 0

Views: 842

Answers (1)

Artavazd
Artavazd

Reputation: 132

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

Upvotes: 1

Related Questions