Q:

How to access data controller / data model from ApiController?

I have a ScoreDataModelsController that contains the following Action Method:
public ActionResult Getnames()
        {
            return View(db.ScoreDataModels.ToList());
        }

In Views I have the corresponding ScoreDataModels folder containing Getnames.cshtml:
@model IEnumerable<WebApplication1.Models.ScoreDataModel>

@{
    ViewBag.Title = "Get Names";
    Layout = "~/Views/Shared/_emptyLayout.cshtml";
}

<table class="table">

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)

            </td>
        </tr>
    }

</table>

This all works fine. Now I would like to make this data (i.e. Names) accessible as json/XML using REST. I have managed to get the ApiController working with the standard settings and by opening http://.../api/Andi i get the values from the string[] in XML format:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace WebApplication1.Controllers
{
    public class AndiController : ApiController
    {

        // GET api/<controller>
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2", "und en dritte" };

  //Here I need help: ScoreDataModelsController sdm = new ScoreDataModelsController();
           // var res = from r in sdm

        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<controller>
        public void Post([FromBody]string value)
        {
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }
}

Now, instead of "value1, value2 ..." I would like to get the names from my ScoreDataModel / ScoreDataModelsController.
The ScoreDataModel looks like this. I have used this model to create the controller and view by scaffolding in Visual Studio:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace WebApplication1.Models
{
    public class ScoreDataModel
    {
        [Key]
        public int ID { get; set; }
        public string Name { get; set; }
        public int Score { get; set; }

    }
}

I would very much appreciate if you could lead me into the right direction to get this REST API working with my existing data controller / data model.

A:

Create a central class which holds your data access logic, something like this:
public class DataRepository
{
    private DatabaseContext db = new DatabaseContext();

    public List<ScoreDataModel> GetNames()
    {
        return db.ScoreDataModels.ToList();
    }
}

Now you can use this class to access your data from both the MVC controller and the api controller:
public class AndiController : ApiController
{
    private DataRepository dbRepo = new DataRepository();

    public IEnumerable<ScoreDataModel> Get()
    {
        List<ScoreDataModel> names = dbRepo.GetNames();
        return names;
    }
}

