繁体   English   中英

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

[英]How to route between two different action methods with same parameters

我正在使用属性路由开发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);
            }
        }

我正在通过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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM