2017-04-04 180 views
-1

我是ASP.net Core和Angular 2的新手。我創建了我的angular 2安裝程序,它的工作正常。但是當我嘗試擊打我的WebApi時,我無法擊中。如何從Angular 2打Asp.net Core Api

namespace Angular2Application8.Controllers 
{ 
    [Produces("application/json")] 
    [Route("api/Hello")]  
    public class HelloController : Controller 
    { 
     StudentContext db = new StudentContext(); 

     [HttpGet]  
     public string Ghouse() 
     { 
      var x = from n in db.StudentMaster 
        select n; 
      return "Hello Ghouse"; 
     } 
    } 
} 

我想在http://localhost:1473/api/Hello/Ghouse

+0

您的Angular應用程序是否由您的API的相同主機託管? –

+0

是的,它託管在相同的 –

+0

請編輯您的問題,以包括您在瀏覽器控制檯中獲得的任何錯誤消息。 –

回答

2

使用[Route("api/[controller]/[action]")]所有Controllers所以當你的controller的名字是HelloControllermethodGHouse您的網址應該是api/hello/ghouse

另外,還要確保你已經正確可配置routing

0

打我的控制器例子中的屬性的路由不允許你打所需的URL。

您需要更新路線模板或更改您呼叫的URL。

[Route("api/Hello")] 
public class HelloController : Controller { 
    StudentContext db = new StudentContext(); 
    [HttpGet] // Matches GET api/Hello 
    [HttpGet("Ghouse")] // Matches GET api/Hello/Ghouse 
    public string Ghouse() { 
     var x = from n in db.StudentMaster 
       select n; 
     return "Hello Ghouse"; 
    } 
} 
相關問題