2016-04-11 43 views
1

找到我這麼新的single page application。我創建一個webapi,你可以在這裏看到:API控制器不能在瀏覽器中的MVC asp.net

using System; 
using System.Collections.Generic; 
using System.Data.Entity; 
using System.Data.Entity.Infrastructure; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Web.Http; 
using System.Web.Http.Description; 
using WebApplication6.Models; 

namespace WebApplication6.Controllers 
{ 
    public class ManageStudentInforAPIController : ApiController 
    { 
     private SchoolManagementEntities db = new SchoolManagementEntities(); 

     // GET: api/ManageStudentsInfoAPI 
     public IQueryable<Student> Get() 
     { 
      return db.Students; 
     } 
    } 
} 

但是,當我把這個網址http://localhost:5411/ManageStudentInforAPI結果是

The resource cannot be found. 

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /ManageStudentInforAPI 

哪裏是我的問題.ANY細節你需要我可以post.i正在使用vs2015

enter image description here

+1

請添加您的路由配置。默認api已達到'api/controller' – Hypnobrew

回答

2

ManageStudentInforAPI之前錯過api。嘗試通過http://localhost:5411/api/ManageStudentInforAPI

,以取代您的網址也可以添加[Route("route")]到控制器或方法,並呼籲http://localhost:5411/api/route

1

打電話給你的API類似這樣的http://localhost:5411/api/ManageStudentInforAPI

,或者如果你想定製的API URL,那麼你可以設置路線這樣

using System; 
using System.Collections.Generic; 
using System.Data.Entity; 
using System.Data.Entity.Infrastructure; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Web.Http; 
using System.Web.Http.Description; 
using WebApplication6.Models; 

namespace WebApplication6.Controllers 
{ 
    public class ManageStudentInforAPIController : ApiController 
    { 
     private SchoolManagementEntities db = new SchoolManagementEntities(); 

     // GET: api/ManageStudentsInfoAPI 
     [Route("api/MyApi")] 
     public IQueryable<Student> Get() 
     { 
      return db.Students; 
     } 
    } 
} 

然後打電話給你的API類似這樣的http://localhost:5411/api/MyApi