VS 2012 MVC应用程序新手,第一次使用RESTful API(目前为at this point in the tutorial)而我似乎无法在Google Chrome中正确显示信息。设置了断点,如我的控制器中的教程所示()函数,我知道它正在调用,当在浏览器中检查网络视图时,it sees both of my entries I'm trying to display。
我的代码几乎与示例完全相同,标题已被切换:
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="body">
<ul id="drug_entries"></ul>
</div>
@section scripts{
<script type="text/javascript">
$(function ()
{
$.getJSON('/api/DrugEntry', function (drugEntriesJsonPayload)
{
$(drugEntriesJsonPayload).each(function (i, item)
{
$('#drug_entries').append('<li>' > + item.NDC + '</li>');
});
});
});
</script>
}
知道可能是什么原因导致它没有看到任何东西?或者,如果我能提供更多信息?谢谢!
我的route.config:
// <auto-generated/>
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MedicationsShortagesDashboard
{
[ExcludeFromCodeCoverage]
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
我的WebApiConfig.cs:
// <auto-generated/>
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Web.Http;
namespace MedicationsShortagesDashboard
{
[ExcludeFromCodeCoverage]
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport();
// To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api
config.EnableSystemDiagnosticsTracing();
}
}
}
答案 0 :(得分:0)
在WebApiConfig.cs中添加此内容
using System.Net.Http.Headers;
using System.Web.Http;
namespace XXX
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Convention-based routing.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}"
);
}
}
}