使用していた通常の古いコントローラーを API コントローラーに変換しようとしていますが、少し問題があります。これらの一連の関数が行うことは、jQuery で、従業員のすべてのユーザー名を含むファイルを反復処理し、ユーザー名ごとに webapi コントローラーの PopulateEmployee メソッドを呼び出し、JSON を返し、結果の div を設定することです。
手動で ..domain../staffinformation/populateemployee/employeeusername に移動する場合
エラーが発生します
This XML file does not appear to have any style information associated with it. The
document tree is shown below.
<Error>
<Message>
The requested resource does not support http method 'GET'.
</Message>
</Error>
表示される div は Umbraco CMS ページの部分的なビューであり、それが問題だとは思いませんが、別の考えがある場合は教えてください。
WebAPIルーティングまたはその他の何かで欠けているものがあるに違いありません。
ご協力いただきありがとうございます。
コーデはこちら。
このメソッドには HttpPost タグがあることに注意してください
public class StaffInformationController : ApiController
{
[System.Web.Http.ActionName("PopulateEmployee")]
[System.Web.Http.HttpPost]
public StaffListing PopulateEmployee(string id)
{
//do error checking on input
StaffListing staffListing = new StaffListing(id);
//populate other fields
return staffListing;
}
}
API コントローラーのルーティング設定
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
'POST' の使用を指定する jQuery 呼び出しは、この関数での再帰呼び出しのトリッキーさを許してください。
function getEmployeeObjectByIndex() {
$.ajax({
url: $('#root').val() + '/api/StaffInformation/PopulateEmployee',
type: 'POST',
async: true,
contentType: 'application/json, charset=utf-8',
data: JSON.stringify({ 'username': lines[i] }),
success: function (staffObject) {
if (!(staffObject.Name == undefined)) {
buildHtmlStrings(staffObject);
}
i++;
getEmployeeObjectByIndex(); //recursive call
}
});
}