Asp.net 如何最好地让MVCWebAPI返回数据以供使用
我刚开始使用Angular和ASP Web Api,遇到了困难,非常欢迎您的帮助 我有一个我正在开发的网站,它有一个新闻页面,以及一个主页上最近项目的新闻提要。它使用angular和一个简单的JSON文件,我手动更新了这个文件,一切都很好。我想写一个MVC4网络应用程序,允许用户更新新闻和一个用于angular连接的Web api 我已经启动了一个试用项目,并让web应用程序项目将CRUD记录连接到SQL Server,所有这些都可以正常工作。我已经基于标准VS模板构建了一个web api,在调试时可以在浏览器中看到JSON数据,但我无法继续工作,我不知道如何进一步调试它。我的模型在两个表之间有一个关系,这似乎使得返回的JSON非常复杂。我想知道最好的方法,我是否需要修改我的控制器以返回我想要的数据,或者修改在global.asax中返回数据的方式?多谢各位 我的模型:Asp.net 如何最好地让MVCWebAPI返回数据以供使用,asp.net,json,angularjs,asp.net-mvc-4,asp.net-web-api,Asp.net,Json,Angularjs,Asp.net Mvc 4,Asp.net Web Api,我刚开始使用Angular和ASP Web Api,遇到了困难,非常欢迎您的帮助 我有一个我正在开发的网站,它有一个新闻页面,以及一个主页上最近项目的新闻提要。它使用angular和一个简单的JSON文件,我手动更新了这个文件,一切都很好。我想写一个MVC4网络应用程序,允许用户更新新闻和一个用于angular连接的Web api 我已经启动了一个试用项目,并让web应用程序项目将CRUD记录连接到SQL Server,所有这些都可以正常工作。我已经基于标准VS模板构建了一个web api,在调
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace admin.Models
{
public class NewsItem
{
public virtual int id { get; set; }
public virtual int NewsTypeId { get; set; }
public virtual string title { get; set; }
public virtual string details { get; set; }
public virtual DateTime date {get; set;}
public virtual string image { get; set; }
public virtual string url { get; set; }
public virtual DateTime? embargodate { get; set; } //force as nullable
public virtual NewsType NewsType { get; set; }
}
public class NewsType
{
public virtual int NewsTypeId { get; set; }
public virtual string description { get; set; }
}
}
我的控制器:
using webapi.Models;
namespace webapi.Controllers
{
public class NewsController : ApiController
{
private DBNewsContext db = new DBNewsContext();
// GET api/News
public IEnumerable<NewsItem> GetNewsItems()
{
var newsitems = db.NewsItems.Include(n => n.NewsType);
return newsitems.AsEnumerable();
}
// GET api/News/5
public NewsItem GetNewsItem(int id)
{
NewsItem newsitem = db.NewsItems.Find(id);
if (newsitem == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return newsitem;
}
返回的json:
[{"NewsType":{"RelationshipManager":{"_relationships":[[]]},"NewsTypeId":1,"description":"Event"},"RelationshipManager":{"_relationships":[{"EntityKey":{"$id":"1","EntitySetName":"NewsTypes","EntityContainerName":"DBNewsContext","EntityKeyValues":[{"Key":"NewsTypeId","Type":"System.Int32","Value":"1"}]}}]},"id":1,"NewsTypeId":1,"title":"Hub Grub","details":"Greek Night","date":"2015-09-28T00:00:00","image":"test.jpg","url":"test.html","embargodate":"2015-09-24T00:00:00"},{"NewsType":{"RelationshipManager":{"_relationships":[[]]},"NewsTypeId":2,"description":"Article"},"RelationshipManager":{"_relationships":[{"EntityKey":{"$id":"2","EntitySetName":"NewsTypes","EntityContainerName":"DBNewsContext","EntityKeyValues":[{"Key":"NewsTypeId","Type":"System.Int32","Value":"2"}]}}]},"id":2,"NewsTypeId":2,"title":"How to write a CV","details":"Find out how to write a great CV","date":"2015-09-24T00:00:00","image":"test2.jpg","url":"test2.html","embargodate":"2015-09-30T00:00:00"}]
角度:
// get news controller
function NewsCtrl($scope, $http) {
//$http.get('js/news.json')
$http.get('http://localhost:49232/api/news')
.success(function (data) { $scope.items = data.data; })
.error(function (data) { console.log('error') });
}
<div ng-controller="NewsCtrl" class="row">
<div ng-repeat="item in items" class="col-md-4 col-sm-12">
<h3>{{ item.title }}</h3>
<h4>{{ item.date }}</h4>
<p>{{ item.details }}</p>
<!--<img class="img-responsive" src="{{ item.image }}" />-->
</div>
</div>
//获取新闻控制器
函数NewsCtrl($scope,$http){
//$http.get('js/news.json')
$http.get('http://localhost:49232/api/news')
.success(函数(数据){$scope.items=data.data;})
.error(函数(数据){console.log('error')});
}
{{item.title}
{{item.date}
{{item.details}}
我的控制器通常返回类型为IHttpActionResult:
public IHttpActionResult Get()
{
var newsitems = db.NewsItems.Include(n => n.NewsType);
return Ok(newsitems);
}
然后,http回调中的“data”对象包含json。如果您只想从模型返回特定数据,则可以创建一个仅包含所需数据的DTO对象,然后将所需值分配给DTO 因此,在控制器中,类似于:
// GET api/News/5
public NewsItemDTO GetNewsItem(int id)
{
NewsItem newsitem = db.NewsItems.Find(id);
if (newsitem == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
var newsitemdto = new NewsItemDto()
{
// map what you want in here
newsitemdto.title = newsitem.title
};
return newsitemdto;
}
IHttpActionResult仅在Web API 2.x及更高版本中受支持。返回对象的类型应该无关紧要,而且操作已返回有效的JSON。感谢您的回复,但我认为我需要Web API 2来实现这两个功能?我认为此解决方案不需要Web API 2。您所要做的就是创建一个类(NewsItemDto),并将您需要的NewsItem中的数据仅放入该类中。这样,您将得到一个更简单的json对象。
// GET api/News/5
public NewsItemDTO GetNewsItem(int id)
{
NewsItem newsitem = db.NewsItems.Find(id);
if (newsitem == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
var newsitemdto = new NewsItemDto()
{
// map what you want in here
newsitemdto.title = newsitem.title
};
return newsitemdto;
}