【发布时间】: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