displaying the files that do not exist in wwwRoot folder

Anjali Agarwal 1,386 Reputation points
2023-11-02T04:50:13.4866667+00:00

I want to display a file using anchor tab that does not exists in root folder. How can I achieve that. I have these two anchor tabs on my cshtml page

<div>
<a href="~/DisplayFiles/Quarter4/Q4-test - Copy_231131_133432504448693604.pdf">test</a><br/>
<a href="C:\test\test6_259418_133433697138557958.pdf">test1</a>
</div>

I can easily open the first file because it exits in wwwRootfolder/ DisplayFiles/Quarter4, but I cannot display the second file because it exits in C:\test drive. In .net core, can I display a file that exists in another folder and not in wwwRoot folder.

any help will be appreciated.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,239 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ping Ni-MSFT 2,405 Reputation points Microsoft Vendor
    2023-11-02T07:09:42.2066667+00:00

    Hi @Anjali Agarwal ,

    Configure the static files middleware in Program.cs like below

    app.UseStaticFiles();         // Serve files from wwwroot as usual
    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider("C:\\test"),
        RequestPath = "/RequestDisk" // URL path
    });
    
    

    Modify your link like below:

    <a href="/RequestDisk/test6_259418_133433697138557958.pdf">test1</a>
    

    Reference:

    Serve files outside of web root


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Rena

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful