问题
New to VS 2012 MVC applications, first time doing RESTful API (currently at this point in the tutorial) and I can't seem to get info to properly display in Google Chrome. Having set breakpoints as shown in the tutorial in my Controller's Get() function, I know it is being called, and when checking the Network View in the browser, it sees both of my entries I'm trying to display.
My code is practically identical to the example, with titles switched out:
@{
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>
}
Any idea what might be the cause of it not seeing anything? Or if there's more information I can provide? Thanks!
My 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 }
);
}
}
}
My 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();
}
}
}
回答1:
Add this in your 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}"
);
}
}
}
来源:https://stackoverflow.com/questions/21793600/cant-find-display-info-in-browser