title: Read a csv file in c# Web Application (MVC)
body: <p>I am new to C# and Visual Studio. I am trying to read a <code>.csv</code> file into my ASP.NET MVC project and can not find the correct tutorials. I have tried multiple different ways and keep getting the same errors on the same pieces of code listed below is the code I have. </p>
  
  <p>Controller </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Linq;
  using System.Threading.Tasks;
  using Microsoft.AspNetCore.Mvc;
  using Fifa19TeamBuilder.Models;
  using System.IO;
  using System.Web;
  using System.Web.Mvc;
  using Controller = Microsoft.AspNetCore.Mvc.Controller;
  using ActionResult = Microsoft.AspNetCore.Mvc.ActionResult;
  using Microsoft.AspNetCore.Hosting.Server;
  
  namespace Fifa19TeamBuilder.Controllers
  {
      public class HomeController : Controller
      {
          public IActionResult Index()
          {
              return View();
          }
  
          public IActionResult About()
          {
              ViewData["Message"] = "Your application description page.";
  
              return View();
          }
  
          public IActionResult Contact()
          {
              ViewData["Message"] = "Your contact page.";
  
              return View();
          }
  
          public IActionResult Privacy()
          {
              return View();
          }
  
          [System.Web.Mvc.HttpPost]
          public ActionResult Team(*HttpPostedFileBase* postedFile)
          {
              List&lt;TeamModel&gt; team = new List&lt;TeamModel&gt;();
              string filePath = string.Empty;
  
              if (postedFile != null)
              {
                  string path = *Server*.MapPath("~/Uploads/");
                  if (!Directory.Exists(path))
                  {
                      Directory.CreateDirectory(path);
                  }
  
                  filePath = path + Path.GetFileName(postedFile.*FileName*);
                  string extension = Path.GetExtension(postedFile.*FileName*);
                  postedFile.*SaveAs*(filePath);
  
                  //Read the contents of CSV file.
                  string csvData = System.IO.File.ReadAllText(filePath);
  
                  //Execute a loop over the rows.
                  foreach (string row in csvData.Split('\n'))
                  {
                      if (!string.IsNullOrEmpty(row))
                      {
                          team.Add(new TeamModel
                          {
                              ID = Convert.ToInt32(row.Split(',')[0]),
                              player_name = row.Split(',')[1],
                              quality = row.Split(',')[2],
                              overall = row.Split(',')[2],
                              club = row.Split(',')[2],
                              league = row.Split(',')[2],
                              nationality = row.Split(',')[2],
                              position = row.Split(',')[2],
                              age = row.Split(',')[2],
                          });
                      }
                  }
              }
  
              return View(team);
          }
  
          [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
          public IActionResult Error()
          {
              return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
          }
      }
  }
  </code></pre>
  
  <p>Model</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  
  namespace Fifa19TeamBuilder.Models
  {
      public class TeamModel
      {
          public int ID { set; get; }
          public string player_name { set; get; }
          public string quality { set; get; }
          public string overall { set; get; }
          public string club { set; get; }
          public string league { set; get; }
          public string nationality { set; get; }
          public string position { set; get; }
          public string age { set; get; }
      }
  }
  </code></pre>
  
  <p>Error </p>
  
  <blockquote>
    <p>CS0246 The type or namespace name 'HttpPostedFileBase' could not be found (are you missing a using directive or an assembly reference?) </p>
  </blockquote>
  
  <p>Error #2:</p>
  
  <blockquote>
    <p>CS0103 The name 'Server' does not exist in current context   </p>
  </blockquote>
  
  <p>Error #3:    </p>
  
  <blockquote>
    <p>CS1061 'HttpPostedFileBaseModelBinder' does not contain a definition for 'SaveAs' and no accessible extension method accepting a first argument of type 'HttpPostedFileBaseModelBinder' could be found (are you missing a using directive or an reference)</p>
  </blockquote>
  
  <p>Can someone please help me all the tutorials I found was for VS 2010, 2015 none for 2017 not sure if that makes a difference. I am just trying to read a csv file from inside the project folder and put it in a <code>List&lt;&gt;</code>. I do not need to upload the file. I have * where it shows the errors.</p>
  
  <p>This is where I got the code:</p>
  
  <p><a href="https://www.aspsnippets.com/Articles/Upload-Read-and-Display-CSV-file-Text-File-data-in-ASPNet-MVC.aspx" rel="nofollow noreferrer">https://www.aspsnippets.com/Articles/Upload-Read-and-Display-CSV-file-Text-File-data-in-ASPNet-MVC.aspx</a></p>