我在DotNetNuke7中使用Angular Js和TypeScript实现了一个模块,我在一个名为customerNew的web项目中实现了我的所有输入表单和js,然后将一个Web APi添加到另一个名为CustomerNewController的项目中,该项目有我的方法但当我点击我的网站项目到Api的URL时,我没有处理过。我已经实现了一个路由映射器,但仍然无法获得成功。
我的路由映射器类如下: -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotNetNuke.Web.Api;
using System.Web.UI.WebControls;
namespace CustomerNewController
{
class RouteMapper : IServiceRouteMapper
{
public void RegisterRoutes(IMapRoute mapRouteManager)
{
mapRouteManager.MapHttpRoute("CustomerNewController", "default", "{controller}/{action}",
new[] { "CustomerNewController" });
}
}
}
这是我的WebApi
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Web.UI;
using System.Xml.Serialization;
using DotNetNuke.Entities.Users;
using System.Web.Services;
using DotNetNuke.Web.Api;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules.Communications;
using DotNetNuke.Entities.Modules;
namespace CustomerNewController
{
public class CreateController : DnnApiController
{
[AllowAnonymous]
[HttpGet]
public HttpResponseMessage HelloWorld()
{
return Request.CreateResponse(HttpStatusCode.OK, "Hello World!");
}
}}
我的网址
http://localhost/dnn7/DesktopModules/CustomerNewController/Api/Create/HelloWorld
这是我的文件夹结构
答案 0 :(得分:3)
我的WebAPI GET方法示例:
[DnnExceptionFilter]
public class AuthorController : DnnApiController
{
#region Public RPC Methods
[HttpGet]
[AllowAnonymous]
public HttpResponseMessage GetAllAuthors()
{
var lstAuthors = AuthorRepository.GetAllAuthors();
return Request.CreateResponse(HttpStatusCode.OK, lstAuthors);
}
[HttpGet]
[AllowAnonymous]
public HttpResponseMessage GetAlphabet()
{
var lstAuthors = AuthorRepository.GetAlphabet();
return Request.CreateResponse(HttpStatusCode.OK, lstAuthors);
}
#endregion
}
服务路线映射器示例(只需执行此操作一次):
public class ServiceRouteMapper : IServiceRouteMapper
{
#region IServiceRouteMapper Implementation
public void RegisterRoutes(IMapRoute mapRouteManager)
{
mapRouteManager.MapHttpRoute("ATKV.Commerce", "default", "{controller}/{action}", new[] { "ATKV.Commerce.Services" });
}
#endregion
}
致电代码:
// Service Paths
var servicesFramework = opts.servicesFramework;
var servicePath = servicesFramework.getServiceRoot('ATKV.Commerce') + 'Author/';
// Bind data
$.ajax({
type: "GET",
cache: false,
url: servicePath + "GetAllAuthors",
beforeSend: servicesFramework.setModuleHeaders
}).done(function (authors) {
if (typeof authors !== "undefined" && authors != null) {
var viewModel = new MasterViewModel(authors);
ko.applyBindings(viewModel, document.getElementById($(containerElement).attr('id')));
} else {
displayMessage("An error occurred", "dnnFormError");
}
}).fail(function (xhr, status) {
displayMessage(status, "dnnFormError");
return null;
});