Home > Mobile >  How do I read a file? (Getting a server does not exist in context error)
How do I read a file? (Getting a server does not exist in context error)

Time:09-18

I found this code on the documentation and it is giving me server doesn't exist in context error(for Server.MapPath()). Is there any other way of reading a file? I tried entering the absolute path of the file I want to read, but it gives null pointer exception. thanks P.S I am using .net core 3.1

@{
var result = "";
Array userData = null;
char[] delimiterChar = {','};

var dataFile = Server.MapPath("~/App_Data/data.txt");

if (File.Exists(dataFile)) {
    userData = File.ReadAllLines(dataFile);
    if (userData == null) {
        // Empty file.
        result = "The file is empty.";
    }
}
else {
    // File does not exist.
    result = "The file does not exist.";
    }
}
<!DOCTYPE html>

<html>
<head>
<title>Reading Data from a File</title>
</head>
<body>
<div>
    <h1>Reading Data from a File</h1>
    @result
    @if (result == "") {
        <ol>
        @foreach (string dataLine in userData) {
        <li>
            User
            <ul>
            @foreach (string dataItem in dataLine.Split(delimiterChar)) {
                <li>@dataItem</li >
            }
            </ul>
        </li>
        }
        </ol>
    }
</div>
</body>
</html>

CodePudding user response:

If you are using MVC project, Why not read the file in Controller, and pass that text via Model? something like below?

public IActionResult Index()
        {
            var result = "";
            Array userData = null;
            char[] delimiterChar = { ',' };

            var dataFile = Server.MapPath("~/App_Data/data.txt");

            if (File.Exists(dataFile))
            {
                userData = File.ReadAllLines(dataFile);
                if (userData == null)
                {
                    // Empty file.
                    result = "The file is empty.";
                }
            }
            else
            {
                // File does not exist.
                result = "The file does not exist.";
            }

            return View(result);
        }

and then on Index.cshtml

@model string
@if (@Model == "") {
   .
   .
   .
}

CodePudding user response:

it is giving me server doesn't exist in context error(for Server.MapPath())

Is there any other way of reading a file?

Please note that Server.MapPath has not been included in ASP.NET Core.

To access the file /App_Data/data.txt, you can refer to the following code snippet.

@using Microsoft.AspNetCore.Hosting

@inject IWebHostEnvironment env

@{
    var result = "";

    Array userData = null;
    char[] delimiterChar = { ',' };
    var dataFile = System.IO.Path.Combine(env.ContentRootPath, @"App_Data\data.txt");

    if (System.IO.File.Exists(dataFile))
    {
        userData = System.IO.File.ReadAllLines(dataFile);
        if (userData == null)
        {
            // Empty file.
            result = "The file is empty.";
        }
    }
    else
    {
        result = "The file does not exist.";
    }
}

You can know more about "working with static files in ASP.NET Core" from this doc:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-5.0#serve-static-files

And if possible, you can put your code logic in controller action method rather than in MVC view.

  • Related