Usm*_*ooq 2 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);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在通过url调用第一个操作方法:
/api/settings/getVersion?PlatformID=1
Run Code Online (Sandbox Code Playgroud)
第二个是:
/api/settings/getBookingSetting?PlatformID=1
Run Code Online (Sandbox Code Playgroud)
但是在这两种情况下,每次都会调用第一个操作方法.如何在方法名称不同但参数具有相同名称和类型(或不同类型)的情况下进行路由?
您的路线定义与您的网址不符:
[Route("{getVersion}")]
Run Code Online (Sandbox Code Playgroud)
这需要一个路由参数"getVersion",所以即使是/api/settings/1匹配的URL也是如此.你应该重写它,使它不是一个参数:
[Route("getVersion")]
public async Task<IHttpActionResult> GetDBVersion([FromUri]string PlatformID)
Run Code Online (Sandbox Code Playgroud)
下一个:
[Route("getBookingSetting/{PlatformID:int}")]
Run Code Online (Sandbox Code Playgroud)
此定义需要PlatformID作为路径(ie /api/settings/getBookingSetting/1)的一部分,但您将其作为查询字符串传递.
您应该将定义更改为:
[Route("getBookingSetting")]
public async Task<IHttpActionResult> GetDBBookingSetting([FromUri]int PlatformID)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
126 次 |
| 最近记录: |