找不到浏览器/显示信息(Can't find/display info in browser

2019-10-28 16:26发布

新VS 2012 MVC应用程序,第一次做的RESTful API(目前在教程了这一点) ,我似乎无法得到的信息在谷歌浏览器中正确显示。 有设置断点,如图中在我的控制器的get()函数的教程中,我知道它被调用,并在浏览器中检查网络视图时, 它看到我的两个条目我想显示 。

我的代码是几乎相同的例子,用的标题切换出来:

@{
    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();
        }
    }
}

Answer 1:

增加这个在您的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}"
            );
        }
    }
}


文章来源: Can't find/display info in browser