2016-07-07 95 views
1

我是WEB API的新手,嘗試爲多個GET操作設置路由。處理多個GET操作Web API

控制器代碼

// Get api/values 
public IEnumerable<tblUser> Get() 
{ 
    //whatever 
} 

// Get api/values/action 
[ActionName("GetByQue")] 
public IEnumerable<tblQue> GetQue() 
{ 
    //whatever 
} 

// Get api/values/action 
[ActionName("GetUserScore")] 
public IEnumerable<tblScore> GetScore(string user) 
{ 
    //whatever 
} 

配置

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

config.Routes.MapHttpRoute(
    name: "DefaultActionApi", 
    routeTemplate: "api/{controller}/{id}", 
    defaults: new { action = "GetByQue" } 
); 

config.Routes.MapHttpRoute(
    name: "DefaultStringApi", 
    routeTemplate: "api/{controller}/{id}", 
    defaults: new { action = "GetUserScore" } 
); 

當我試着使用http://localhost:54118/api/remote/GetByQue URL收到此錯誤

{ 
    "Message": "The request is invalid.", 
    "MessageDetail": "The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.String Get(Int32)' in 'HydTechiesApi.Controllers.HydTechiesApiController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter." 
} 

是我的路由錯誤?任何幫助將是有價值的,因爲我無法找到解決方案。

+0

看一看容斯的swer http://stackoverflow.com/questions/17184798/asp-net-web-api-routing-not-hitting-custom-action/17187762#17187762 –

回答

0
  1. 您製作了在您的路線中發生了幾次錯誤。正如下面的實施例的代碼:

    config.Routes.MapHttpRoute(
        name: "DefaultActionApi", 
        routeTemplate: "api/{controller}/{id}", 
        defaults: new { action = "GetByQue" }); 
        //url: http://localhost:54118/api/remote/GetByQue 
    

(1)。 routeTemplate:「api/{controller}/{id}」。你指定你的路線有一個ID,它不是可選的。所以你的網址必須有ID。這就是你的錯誤showed.you可以與問題處理類似如下:

defaults: new { id = RouteParameter.Optional } 

(2)。 默認值:new {action =「GetByQue」});你沒有在你的routeTemplate中說出任何關於動作的事情。你的默認左右動作,其中不是有任何意義。

(3)。從您的路線,你網址應該像http://localhost:54118/api/remote/5,你不應該得到多發的路線get方法。

  • 有一些解決方案,你可以使用:
  • (1)。改變路線如下:

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

    請在每個方法中添加[HttpGet]只是因爲。

     [HttpGet] 
        public IEnumerable<tblUser> Get() 
         {......} 
    
        [HttpGet] 
        [ActionName("GetByQue")] 
        public IEnumerable<tblQue> GetQue() 
        {......} 
    
        [HttpGet] 
        [ActionName("GetUserScore")] 
         public IEnumerable<tblScore> GetScore(string user) 
         {......} 
    

    現在你可以使用URL像http://localhost:54118/api/remote/GetByQue

    非常有用的技巧:使用[路線( 「」)]標記指定參數 **你也必須改變路線像上述(1)

    在控制器,請註明要求的方法,以確保get方法

    [HttpGet] 
        [ActionName("GetUserScore")] 
        [Route("api/remote/GetScore/{user}")] 
        public IEnumerable<tblScore> GetScore(string user) 
        {.....} 
        //URL: http://localhost:54118/api/remote/GetScore/user 
    
    +0

    感謝您的好解釋。 – NNR

    1

    你應該在第二配置中添加{action}routeTemplate而不是{id}

    config.Routes.MapHttpRoute(
        name: "DefaultActionApi", 
        routeTemplate: "api/{controller}/{action}", 
        defaults: new { action = "GetByQue" } 
    ); 
    

    你也可以嘗試在動作使用路線attribure:

    [ActionName("GetByQue")] 
    [Route("api/remote/GetByQue")] 
    public IEnumerable<tblQue> GetQue() 
    { 
        //whatever 
    } 
    

    或變更單(第二配置和第一種配置)配置WebApiConfig.cs

    +0

    感謝您的回覆。但得到同樣的問題。 – NNR

    +0

    @NNR請參閱我的更新 – Marusyk