[英]ASP.net N tier Architecture presentation layer
我遇到一個我不知道表示層如何使用服務層的問題。 我總共有三層:數據層,服務層和表示層。
在數據層中,我使用了Ado.net並創建了存儲庫。
在服務層中,我創建了Dto實體。 當我從Controller呼叫服務時遇到問題。
錯誤是:
沒有為此對象定義無參數構造函數。 說明:執行當前Web請求時出現未處理的異常。 查看堆棧跟蹤以查看有關此錯誤的更多信息,並確定導致代碼錯誤的位置。 異常詳細信息:System.MissingMethodException:沒有為此對象定義無參數構造函數。
源錯誤:
運行當前Web請求時生成了未處理的異常。 可以使用異常堆棧查看有關異常的來源和位置的信息。
下面是我的代碼:
using AutoMapper;
using OA.Models;
using OA.Service;
using OA.Service.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace OA.Controllers
{
public class HomeController : Controller
{
private readonly IUserService userService;
private readonly IUserProfileService userProfileService;
public HomeController(IUserService userService, IUserProfileService userProfileService)
{
this.userService = userService;
this.userProfileService = userProfileService;
}
// GET: Home
[HttpGet]
public ActionResult Index()
{
List<UserViewModel> model = new List<UserViewModel>();
userService.GetUsers().ToList().ForEach(u =>
{
UserProfileDto userProfileDto = userProfileService.GetUserProfile(u.Id);
UserViewModel user = new UserViewModel
{
Id = u.Id,
Name = $"{userProfileDto.FirstName} {userProfileDto.LastName}",
Email = u.Email,
Address = userProfileDto.Address
};
model.Add(user);
});
return View(model);
}
}
}
有人知道這是什么問題嗎? 還是我誤會了如何使用N層架構? 謝謝
您必須在IOC中注冊HomeController
。
以Unity為例:
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<Controllers.HomeController>(new InjectionConstructor());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.