我似乎在兜圈子,进入调试模式似乎根本没有帮助。查看成员的控制器和模型,我看不到任何重复的签名(下面的代码块)并且 ajax 调用(JQuery)看起来对我来说很容易找到。如果有人能指出正确的方向,我将不胜感激。
我可以看到正在对 API 进行调用,并且它返回代码 500,完整错误如下。
我还尝试过清理、重建、硬清除缓存、重新启动应用程序池以及重新启动我正在运行的计算机。主要是为了尽可能多地杀死稻草。
我得到的完整错误是
{
"Message": "An error has occurred.",
"ExceptionMessage": "Multiple actions were found that match the request: \r\nGet on type Portal.api.MembersController\r\nGet on type Portal.api.MembersController",
"ExceptionType": "System.InvalidOperationException",
"StackTrace": " at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)\r\n at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()"
}
JQuery / Javascript 调用
function fetchMemberDetails(el) {
var puid = $(el).val();
var pattern = /^[a-z\'\-]+[0-9]{3}$/i;
if (pattern.test(puid)) {
$.ajax({
type: 'GET',
url: baseMembersURL + puid,
dataType: "json",
success: function (data) {
$.map(JSON.parse(data), function (i, item) {
('#spanNewName').text(item.Name);
})
}
});
}
}
会员控制器
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Results;
using System.Web.Script.Serialization;
namespace Portal.api
{
public class MembersController : ApiController
{
JavaScriptSerializer jss = new JavaScriptSerializer
{
MaxJsonLength = int.MaxValue
};
// GET api/<controller>
public void Get()
{
}
public JsonResult<string> Get(object id)
{
if (Central.GetSession("PermissionLevel", "PORTAL") == "" && Central.GetSession("PermissionLevel", "PORTAL").All(char.IsDigit) && int.Parse(Central.GetSession("PermissionLevel", "PORTAL")) >= 2)
{
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
Models.TeamViewer.Member member = new Models.TeamViewer.Member(id);
row = new Dictionary<string, object>
{
//return objects
};
rows.Add(row);
return Json(jss.Serialize(rows));
}
else
{
// Error handling if conditions not met
}
}
// POST api/<controller>
public void Post([FromBody] string value)
{
}
// PUT api/<controller>/5
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/<controller>/5
public JsonResult<string> Delete(int id, [FromBody] string value)
{
if (Central.GetSession("PermissionLevel", "PORTAL") != "" && Central.GetSession("PermissionLevel", "PORTAL").All(char.IsDigit) && int.Parse(Central.GetSession("PermissionLevel", "PORTAL")) >= 2)
{
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
Models.TeamViewer.Member member = new Models.TeamViewer.Member(id);
//Delete Member
}
return Json(jss.Serialize(value));
}
}
}
会员模型
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using Portal.Modules;
namespace Portal.Models.TeamViewer
{
public class Member
{
private string sql;
private DataTable dt = new DataTable();
private readonly List<SqlParameter> sqlParams = new List<SqlParameter>();
public int ID { get; set; }
public string PUID { get; set; }
public string Name { get; set; }
public string Skills { get; set; }
public List<Team> Teams { get; set; }
public Member()
{
}
public Member(object id)
{
if (id.GetType() == typeof(int))
{
ID = (int)id;
FetchByID();
}
else if (id.GetType() == typeof(string))
{
PUID = (string)id;
FetchByPUID();
}
}
public Member(string puid, string name, string skills)
{
ID = 0;
PUID = puid;
Name = name;
Skills = skills;
}
public void FetchByID()
{
sqlParams.Clear();
sqlParams.Add(new SqlParameter("@ID", ID));
sql = "SELECT PUID, Name, Skills, FkRank, LongRank, ShortRank " +
" FROM TeamPlanner_Members" +
" WHERE ID = @ID";
dt = SQLDB.FetchMultipleValues(sql, Central.userDB, sqlParams);
if (dt.Rows.Count > 0)
{
PUID = (string)dt.Rows[0]["PUID"];
Name = (string)dt.Rows[0]["Name"];
Skills = (string)dt.Rows[0]["Skills"];
Teams = GetTeamList();
}
}
public void FetchByPUID()
{
sqlParams.Clear();
sqlParams.Add(new SqlParameter("@PUID", PUID));
sql = "SELECT ID, Name, Skills " +
" FROM TeamPlanner_Members" +
" WHERE PUID = @PUID";
dt = SQLDB.FetchMultipleValues(sql, Central.userDB, sqlParams);
if (dt.Rows.Count > 0)
{
ID = (int)dt.Rows[0]["ID"];
Name = (string)dt.Rows[0]["Name"];
Skills = (string)dt.Rows[0]["Skills"];
Teams = GetTeamList();
}
}
public void Save()
{
sqlParams.Clear();
sqlParams.Add(new SqlParameter("@PUID", PUID));
sqlParams.Add(new SqlParameter("@Name", Name));
sqlParams.Add(new SqlParameter("@Skills", Skills));
if (ID != 0)
{
sql = "INSERT INTO TeamPlanner_Members(PUID, Name, Skills) " +
" VALUES(@PUID, @Name, @Skills)";
SQLDB.ExecuteSQL(sql, Central.userDB, sqlParams);
SaveTeamList();
}
else
{
string ret;
sqlParams.Add(new SqlParameter("@ID", ID));
sql = "UPDATE TeamPlanner_Members " +
" SET PUID = @PUID" +
" , Name = @Name" +
" , Skills = @Skills" +
" WHERE ID = @ID";
ret = SQLDB.ExecuteSQL(sql, Central.userDB, sqlParams);
if (ret.All(char.IsNumber))
{
ID = int.Parse(ret);
SaveTeamList();
}
}
}
private List<Team> GetTeamList()
{
List<Team> teamList = new List<Team>();
sqlParams.Clear();
sqlParams.Add(new SqlParameter("@Member", ID));
sql = "SELECT FkTeam FROM TeamPlanner_Team_Member WHERE FkMember = @Member ORDER BY Ordering";
dt = SQLDB.FetchMultipleValues(sql, Central.userDB, sqlParams);
foreach (DataRow row in dt.Rows)
{
teamList.Add(new Team((int)row["FkTeam"]));
}
return teamList;
}
private void SaveTeamList()
{
sqlParams.Clear();
sqlParams.Add(new SqlParameter("@Member", ID));
sql = "DELETE FROM TeamPlanner_Team_Member WHERE FkMember = @Member";
SQLDB.ExecuteSQL(sql, Central.userDB, sqlParams);
foreach (Team team in Teams)
{
sqlParams.Clear();
sqlParams.Add(new SqlParameter("@Member", ID));
sqlParams.Add(new SqlParameter("@Team", team.ID));
sql = "INSERT INTO TeamPlanner_Team_Members(FkMember, FkTeam) VALUES (@Member, @Team)";
SQLDB.ExecuteSQL(sql, Central.userDB, sqlParams);
}
}
public void AddToTeamList(int teamID)
{
Teams.Add(new Team(teamID));
}
public void RemoveFromTeamList(int teamID)
{
Teams.Remove(new Team(teamID));
}
}
}
全球航线注册
routes.MapHttpRoute(
name: "APIs",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
您定义了两个 Get 方法,但没有为它们指定不同的路由:
我会用这样的东西:
// GET api/members
[HttpGet]
[Route("")]
public IHttpActionResult Get()
{
// Implementation for a generic Get request
return Ok("Generic GET response");
}
// GET api/members/{id}
[HttpGet]
[Route("{id}")]
public JsonResult<string> GetById(object id)
{
// insert your code here.....
}