2014-02-15 57 views
0

VS 2012 MVC應用程序的新手,第一次使用RESTful API(目前爲at this point in the tutorial),我似乎無法獲取在Chrome瀏覽器中正確顯示的信息。教程在我的控制器的get()函數,我知道它被調用,並檢查網絡視圖在瀏覽器中,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

是的,你可以向我們提供您的路線.config,還是你沒有配置它? –

+0

我還沒有,在這裏,可能錯過了一步? http://pastebin.com/SQ56A1BC – Befall

回答

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}" 
      ); 
     } 
    } 
} 
+0

這是說路由「DefaultApi」已經在路由集合中,但我不知道它在哪裏。另外,我的web api簽名是什麼?我對此非常重視。 – Befall

+0

嗯,這段代碼已經在WebApiConfig.cs中了,那是你認爲丟失的東西嗎? – Befall

+0

仔細看看路由模板,它以/ api而不是控制器開頭 –