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

How to route between two different action methods with same parameters

我正在开发具有属性路由的 ASP.NET Web API 2。 我有一个控制器 class 和两个操作方法:

[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);
            }
        }

我正在通过 url 调用第一个操作方法:

/api/settings/getVersion?PlatformID=1

第二个作者:

/api/settings/getBookingSetting?PlatformID=1

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

您的路由定义与您的网址不匹配:

[Route("{getVersion}")]

这需要一个路由参数 "getVersion",因此即使像 /api/settings/1 这样的 URL 也会匹配。您应该重写它,使其不是参数:

[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)