【问题标题】:How to access data controller / data model from ApiController?如何从 ApiController 访问数据控制器/数据模型?
【发布时间】:2016-06-12 21:32:59
【问题描述】:

我有一个 ScoreDataModelsController 包含以下操作方法:

public ActionResult Getnames()
        {
            return View(db.ScoreDataModels.ToList());
        }

在视图中,我有相应的 ScoreDataModels 文件夹,其中包含 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>

这一切都很好。现在我想使用 REST 将这些数据(即名称)作为 json/XML 访问。我设法让 ApiController 使用标准设置并通过打开 http://.../api/Andi 我从字符串 [] 中以 XML 格式获取值:

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

现在,我想从我的 ScoreDataModel / ScoreDataModelsController 中获取名称,而不是“value1, value2 ...”。

ScoreDataModel 如下所示。我已经使用这个模型在 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; }

    }
}

如果您能引导我进入正确的方向,让这个 REST API 与我现有的数据控制器/数据模型一起工作,我将不胜感激。

【问题讨论】:

    标签: asp.net asp.net-mvc rest


    【解决方案1】:

    创建一个包含数据访问逻辑的中心类,如下所示:

    public class DataRepository
    {
        private DatabaseContext db = new DatabaseContext();
    
        public List<ScoreDataModel> GetNames()
        {
            return db.ScoreDataModels.ToList();
        }
    }
    

    现在您可以使用此类从 MVC 控制器和 api 控制器访问您的数据:

    public class AndiController : ApiController
    {
        private DataRepository dbRepo = new DataRepository();
    
        public IEnumerable<ScoreDataModel> Get()
        {
            List<ScoreDataModel> names = dbRepo.GetNames();
            return names;
        }
    }
    

    【讨论】:

    • 听起来很有希望,谢谢。在哪个命名空间中可以找到 DatabaseContext?
    • 这是您在 MVC 控制器中使用的“db”对象。实体框架数据库上下文类。如果对您有帮助,请标记为已回答
    • 如何访问这个对象?在我的 ScoreDataModelsController 中,使用“private ApplicationDbContext db = new ApplicationDbContext()”创建了一个上下文 - 如何从 DataRepository.cs 访问这个 db 变量?
    • 简单地实例化私有 ApplicationDbContext db = new ApplicationDbContext();在 DataRepository 类中
    【解决方案2】:

    使用这个

    var data= db.ScoreDataModels.ToList()
    
                    List<String>list=new List<String>();
    
                    foreach(var r in data)
                    {
                        list.add(r.Name);
    
                    }
    
    
                    return list;
    

    【讨论】:

    • 如果需要任何帮助,请发表评论
    • 也很完美 - 非常感谢。我只需要使用 'ApplicationDbContext db = new ApplicationDbContext();' 创建 Databese 上下文
    猜你喜欢
    • 2017-02-06
    • 1970-01-01
    • 2017-05-08
    • 2017-10-06
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    相关资源
    最近更新 更多