简体   繁体   English

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

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

I am developing ASP.NET Web API 2 with Attribute Routing. 我正在使用属性路由开发ASP.NET Web API 2。 I have a controller class and two action methods in it: 我有一个控制器类和两个操作方法:

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

i am calling the first action method by url: 我正在通过url调用第一个操作方法:

/api/settings/getVersion?PlatformID=1

and second one by: 第二个是:

/api/settings/getBookingSetting?PlatformID=1

however in both cases, the first action method gets called everytime. 但是,在两种情况下,第一个操作方法都会被调用。 How can i route between such cases where method names are different but parameters are of same name and type(or of different types)? 在方法名称不同但参数具有相同名称和类型(或不同类型)的情况下,如何进行路由?

Your route definitions do not match your URLs: 您的路线定义与您的网址不匹配:

[Route("{getVersion}")]

This expects a route parameter "getVersion", so even URLs like /api/settings/1 will match. 这需要一个路由参数“ getVersion”,因此即使是/api/settings/1类的URL也将匹配。 You should rewrite it so that it's not a parameter: 您应该重写它,使其不是参数:

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

Next: 下一个:

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

This definition expects PlatformID as part of the route (ie /api/settings/getBookingSetting/1 ), but you're passing it as a query string. 此定义期望PlatformID作为路由的一部分(即/api/settings/getBookingSetting/1 ),但是您将其作为查询字符串传递。

You should change the definition to this: 您应该将定义更改为此:

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

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

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