本人多年winform經驗,但是web方面做得很少,現在在用ABP做Web程序,以下是一些心得體會。可能彎路比較多。。。
現在有個需求是使用ajax加載jsTree做導航,這里就需要創建webapi了,參考了一些技術文檔:
基於DDD的現代ASP.NET開發框架--ABP系列之2、ABP入門教程
在Application下定義了如下接口和類用於測試:
public interface INaviTreeAppService : IApplicationService
{
ListResultOutput<NaviTreeOutput> GetNaviTreeOutputs();
//[HttpGet]
NaviTreeOutput GetTopNode();
}
public class NaviTreeManagerAppService : ApplicationService, INaviTreeAppService
{
private readonly IRepository<DepartMent> _departmentRepository;
public NaviTreeAppService(IRepository<DepartMent> departmentRepository )
{
_departmentRepository = departmentRepository;
}
public ListResultOutput<Dto.NaviTreeOutput> GetNaviTreeOutputs()
{
var list = new List<Dto.NaviTreeOutput>();
var opt = new Dto.NaviTreeOutput();
opt.id = "1";
opt.parent = "#";
opt.text = "監控單位1";
opt.state=new StateObj(){selected=true,opened=true};
list.Add(opt);
opt = new Dto.NaviTreeOutput();
opt.id = "2";
opt.parent = "#";
opt.text = "監控單位2";
opt.state = new StateObj() { opened = true };
list.Add(opt);
opt = new Dto.NaviTreeOutput();
opt.id = "3";
opt.parent = "1";
opt.text = "監控子單位11";
opt.state = new StateObj() { opened = true };
list.Add(opt);
opt = new Dto.NaviTreeOutput();
opt.id = "4";
opt.parent = "1";
opt.text = "監控子單位12";
opt.state = new StateObj() { opened = true };
list.Add(opt);
opt = new Dto.NaviTreeOutput();
opt.id = "5";
opt.parent = "2";
opt.text = "監控子單位21";
opt.state = new StateObj() { opened = true };
list.Add(opt);
opt = new Dto.NaviTreeOutput();
opt.id = "6";
opt.parent = "2";
opt.text = "監控子單位22";
opt.state = new StateObj() { opened = true };
list.Add(opt);
return new ListResultOutput<NaviTreeOutput>(list);
}
public NaviTreeOutput GetTopNode()
{
var opt = new Dto.NaviTreeOutput();
opt.id = "1";
opt.parent = "#";
opt.text = "監控單位1";
opt.state = new StateObj() { selected = true, opened = true };
return opt;
}
}
在webapimodule中添加如下解析,將Post改為Get訪問:
DynamicApiControllerBuilder.For<INaviTreeAppService>("DepartAndNavi/NaviTree").ForMethod("GetTopNode").WithVerb(HttpVerb.Get).Build();
在瀏覽器中輸入路徑后,發現報錯:{"message":"An error has occurred."}
打開地址:http://localhost:6634/api/abpServiceProxies/GetAll 發現確實有此api,但是訪問就是報錯。
重新讀了好幾遍相關文檔,對比了現有的abpzero定義,都沒有發現什么。
但是看到webapimodule文件的動態解析:
DynamicApiControllerBuilder
.ForAll<IApplicationService>(typeof(WebMonitorApplicationModule).Assembly, "app")
.Build();
我覺得我寫了application層的定義,那么就應該解析出我的INaviTreeAppService中定義的api,所以我刪除了自定義的解析,
然后訪問http://localhost:6634/api/abpServiceProxies/GetAll查看,發現沒有INaviTreeAppService的webapi定義。
這時終於知道具體的錯誤地點了,肯定是application層寫的不對,又是讀文檔又是對比代碼。
終於讓我發現了錯誤的地方了:Zero寫的都是對應的,比如ITenantAppService和TenantAppService;IRoleAppService和RoleAppService
而我的INaviTreeAppService 和 NaviTreeManagerAppService 則不是如此,然后將NaviTreeManagerAppService 改為NaviTreeAppService后成功了。
看來abp也繼承了mvc中默認規則的設置,IAppService跟實現類AppService必須是名稱相同。
