如何在具有相同参数的两种不同操作方法之间进行路由

时间:2018-06-12 07:20:55

标签: c# asp.net-web-api asp.net-web-api2

我正在使用属性路由开发ASP.NET Web API 2。 我有一个控制器类和两个动作方法:

[RoutePrefix("api/settings")]
    public class SettingsController : ApiController
    {
        [Route("{getVersion}")]
        public async Task<IHttpActionResult> GetDBVersion(string PlatformID)
        {
            try
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                SqlManager sqlManager = new SqlManager();
                DataTable dt = sqlManager.GetLocalDBVersion(PlatformID);

                List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                Dictionary<string, object> row;
                foreach (DataRow dr in dt.Rows)
                {
                    row = new Dictionary<string, object>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        row.Add(col.ColumnName, dr[col]);
                    }
                    rows.Add(row);
                }
                return Ok(serializer.Serialize(rows));
            }
            catch (Exception ex)
            {
                return Content(HttpStatusCode.ExpectationFailed, ex.Message);
            }
        }
        [Route("getBookingSetting/{PlatformID:int}")]
        public async Task<IHttpActionResult> GetDBBookingSetting(int PlatformID)
        {
            try
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                SqlManager sqlManager = new SqlManager();
                DataTable dt = sqlManager.GetBookingSetting(PlatformID.ToString());

                List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                Dictionary<string, object> row;
                foreach (DataRow dr in dt.Rows)
                {
                    row = new Dictionary<string, object>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        row.Add(col.ColumnName, dr[col]);
                    }
                    rows.Add(row);
                }
                return Ok(serializer.Serialize(rows));
            }
            catch (Exception ex)
            {
                return Content(HttpStatusCode.ExpectationFailed, ex.Message);
            }
        }

我通过网址调用第一个操作方法:

/api/settings/getVersion?PlatformID=1

和第二个:

/api/settings/getBookingSetting?PlatformID=1

然而,在这两种情况下,每次都会调用第一个操作方法。 如何在方法名称不同但参数具有相同名称和类型(或不同类型)的情况下进行路由?

1 个答案:

答案 0 :(得分:5)

您的路线定义与您的网址不符:

[Route("{getVersion}")]

这需要一个路由参数&#34; getVersion&#34;,所以即使像/api/settings/1这样的网址也会匹配。你应该重写它,使它不是一个参数:

[Route("getVersion")]
public async Task<IHttpActionResult> GetDBVersion([FromUri]string PlatformID)

下一步:

[Route("getBookingSetting/{PlatformID:int}")]

此定义期望PlatformID作为路线的一部分(即/api/settings/getBookingSetting/1),但您将其作为查询字符串传递。

您应该将定义更改为:

[Route("getBookingSetting")]
public async Task<IHttpActionResult> GetDBBookingSetting([FromUri]int PlatformID)