2016-09-28 2 views
0

안녕하세요 저는 VS2015에서 WebApi를 배우고 있습니다. 나는 MVC4에서 몇 가지 경험을 가지고 있으므로 MVC $에서 라우팅의 개념을 알고있다. 나는 http://www.c-sharpcorner.com/uploadfile/65794e/web-api-with-angular-js/ 웹 사이트를 팔로우하고있다. 나는 아래의 데이터베이스에서 일부 데이터를 표시하려고합니다.WebApi를 실행하는 방법?

public class TestController : ApiController 
    { 
     // GET: api/Test 
     public WebAPI db = new WebAPI(); 
     public IEnumerable<LoginTbl> Get() 
     { 
      return db.LoginTbls.AsEnumerable(); 
     } 
    } 

아래의 WebApiConfig.cs와 같습니다.

config.Routes.MapHttpRoute(
       name: "DefaultApi", 
       routeTemplate: "api/{Test}/{id}", 
       defaults: new { id = RouteParameter.Optional } 
      ); 

Service.Js 코드.

app.service("APIService", function ($http) { 
    this.getSubs = function() { 

     return $http.Get("") 
    } 
} 
); 

나는이 내 index.cshtml

@{ 

} 

<style> 
    table, tr, td, th { 
     border: 1px solid #ccc; 
     padding: 10px; 
     margin: 10px; 
    } 
</style> 
<h2>Welcome to Sibeesh Passion's Email List</h2> 
<body data-ng-app="APIModule"> 
    <div id="tblSubs" ng-controller="APIController"> 
     <table> 
      <tr> 
       <th>UserName</th> 
       <th>Password</th> 

      </tr> 
      <tbody data-ng-repeat="sub in subscriber"> 
       <tr> 
        <td>{{sub.UserName}}</td> 
        <td>{{sub.Password}}</td> 

       </tr> 
      </tbody> 
     </table> 
    </div> 
</body> 

<script src="~/Scripts/angular.js"></script> 
<script src="~/Scripts/angular-route.js"></script> 
<script src="~/Scripts/APIScripts/Module.js"></script> 
<script src="~/Scripts/APIScripts/Service.js"></script> 
<script src="~/Scripts/APIScripts/Controller.js"></script> 

내가 clientscript로 AngularJS와를 사용하고있다 ("") $ http.Get을에 통과 할 것을 확실하지 않다. 두 RouteConfig.cs 및 WebAPiconfig.cs를 볼 수 있으므로 위의 응용 프로그램을 실행하는 방법을 잘 모르겠습니다. 누군가가 응용 프로그램을 실행하기 위해 어떤 파일을 변경해야하는지 알려줍니다. 고맙습니다 ...

+0

다음과 같은 튜토리얼이 많이 있습니다 : http://www.asp.net/web-api/overview/older-versions/creating-a-web-api-that-supports-crudoperations – Tester

+0

고마워요. XML 형식으로 필요한 결과를 얻고 있습니다. 내가 뭔가 잘못하고 있는거야? –

+0

WebApiConfig 클래스에서이를 설정해야합니다. 'config.Formatters.JsonFormatter.SupportedMediaTypes.Add (new MediaTypeHeaderValue ("text/html"));' –

답변

0

자습서에서는 다음과 같이 API 컨트롤러 이름을 실제로 말하지 않지만 SubscriberController이라고합니다. 경로는 api/과 일치하고 단어 Controller - 그래서 자습서의 api/Subscriber 앞에있는 단어와 일치하고 api/Test 경우에 맞 춥니 다. 당신의 각 라우팅 구성에서

, 코드가 웹 API에서 JSON을 반환하기 위해 $http.get("api/Test");

이 될 것입니다, 우리는 당신의 WebApiConfig.cs에 코드 줄이 필요합니다 :

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

+1

고마워요. Json 형식으로 데이터를 가져올 수 있습니다. –

관련 문제