我在DNN中创建了一个Web API,其中提供了示例 http://www.dnnsoftware.com/community-blog/cid/142400/getting-started-with-services-framework-webapi-edition 在这个例子中,我修改了HelloWorld方法以获取参数,但是在通过ajax时无法获取参数。这是我的代码
public class RouteMapper : IServiceRouteMapper
{
public void RegisterRoutes(IMapRoute mapRouteManager)
{
mapRouteManager.MapHttpRoute(
moduleFolderName: "MyServices",
routeName: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional },
namespaces: new[] { "MyServices" }
);
}
}
public class WelcomeController : DnnApiController
{
[AllowAnonymous]
[HttpGet]
public string HelloWorld(WelcomeParameter id)
{
return "test" + id.UserID + id.ClientID + id.LanguageID;
}
}
public class WelcomeParameter
{
public int UserID;
public int ClientID;
public int LanguageID;
}
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
method: "GET",
url: "desktopmodules/myservices/apI/Welcome/HelloWorld/",
data: JSON.stringify({"id":{ "UserID": 1, "ClientID": 1, "LanguageID": 1}}),
contentType: 'application/json; charset=utf-8',
dataType: 'json'
})
.done(function (msg) {
alert("Content: " + msg);
});
});
</script>
此操作不会将输出返回为
test111
test111
答案 0 :(得分:0)
我收到错误,因为405方法不允许“请求的资源 不支持http方法'POST'。“
您也希望使用HTTP POST添加操作方法。
public class WelcomeController : DnnApiController
{
[AllowAnonymous]
[HttpGet]
public string HelloWorld()
{
return "";
}
[AllowAnonymous]
[HttpPost]
public string HelloWorld(WelcomeParameter id)
{
return "test" + id.UserID + id.ClientID + id.LanguageID;
}
}