asp.net-mvc - 如何修复 "Exception of type ' Unity.Exceptions.InvalidRegistrationException' 被抛出”

标签 asp.net-mvc

我尝试登录我的应用程序,但它抛出此异常。我把它留给了正确运行的应用程序。两天后我开始重新处理这个问题,所以现在它给出了这个异常(exception)。

我已在 2 天前申请了工作单元。现在,今天在检查应用程序之前,我尝试通过添加“项目已存在”的操作方法来添加验证。然后我删除了这段代码,仍然给出了这个异常。没明白。帐户 Controller 存在问题,因为它不允许登录。

 public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();

        // register all your components with the container here
        // it is NOT necessary to register your controllers

        // e.g. container.RegisterType<ITestService, TestService>();
        container.RegisterType<ICategoryRepository, CategoryRepository>();
        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }

这是 UnityConfig。

 public class CategoryViewModel
{
    public int CategoryId { get; set; }

    [Required]
    [Remote("IsCategoryExists", "Category", HttpMethod = "POST", ErrorMessage = "This Category already exist.", AdditionalFields = "CategoryName,CategoryId")]
    [Display(Name ="Category Name")]
    public string CategoryName { get; set; }

    [Display(Name = "Is Active")]
    public bool IsActive { get; set; }
}

这是类别的模型

public class CategoryController : AdminBaseController
{
    LaundryManagementSystemEntities db = new LaundryManagementSystemEntities();
    //private ICategoryRepository interfaceobj;

    private UnitOfWork unitOfWork;

    public CategoryController()
    {
       // this.interfaceobj = iCategory;
        //For Repositorypatterns
        //this.interfaceobj = new CategoryRepository(new LaundryManagementSystemEntities());

        //For Unit Of Work 
       this.unitOfWork = new UnitOfWork(new LaundryManagementSystemEntities());
    }

    // GET: Category        
    public ActionResult Index()
    {
        return View();
    }
    public ActionResult Create()
    {
        CategoryViewModel categoryViewModel = new CategoryViewModel();
        return PartialView("Create",categoryViewModel);
    }

    [HttpPost]
    public ActionResult Create(CategoryViewModel category)
    {
        if (ModelState.IsValid)
        { 
            unitOfWork.CategoryRepository.CreateCategory(category);
           // interfaceobj.CreateCategory(catogery);
        }
        return PartialView("Create",category);
    }

    //[HttpPost]
    //public JsonResult IsCategoryExists(string CategoryName, int CategoryId)
    //{
    //    bool found = false;

    //    if (CategoryId == 0)
    //    {
    //        found = db.Categories.Any(na => na.CategoryName == CategoryName);
    //    }
    //    else
    //    {

    //        found = db.Categories.Any(na => na.CategoryName == CategoryName && na.CategoryID != CategoryId);
    //    }
    //    if (!found)
    //    {
    //        return Json(true, JsonRequestBehavior.AllowGet);
    //    }
    //    return Json(false, JsonRequestBehavior.AllowGet);
    //}

这是我添加 IsCategoryExists 方法的 Controller ,暂时注释掉。

 ublic class UnitOfWork: IDisposable
{
    private CategoryRepository categoryRepository;
    private LaundryManagementSystemEntities _entities;

    public UnitOfWork(LaundryManagementSystemEntities entities)
    {
        this._entities = entities;
    }

    public CategoryRepository CategoryRepository
    {
        get
        {
            if (categoryRepository == null)
            {
                categoryRepository = new CategoryRepository(_entities);
            }
            return categoryRepository;
        }
    }

    public void Dispose()
    {
        _entities.Dispose();
    }

    public void Save()
    {
        _entities.SaveChanges();
    }
}

工作单元类

  public class CategoryRepository : ICategoryRepository
{
    private LaundryManagementSystemEntities context;

    public CategoryRepository(LaundryManagementSystemEntities db)
    {
        this.context = db;
    }

    public void CreateCategory(CategoryViewModel categoryViewModel)
    {
        var category = new Category();
        category.CategoryName = categoryViewModel.CategoryName;
        category.IsActive = categoryViewModel.IsActive;
        context.Categories.Add(category);
        context.SaveChanges();
    }

    public void DeleteCategory(int categoryId)
    {

        Category category = context.Categories.Find(categoryId);
        if (category != null)
        {
            context.Categories.Remove(category);
            context.SaveChanges();
        }
    }

    public void DeleteProductOfCategory(int productId)
    {
         var listProducts = context.Products.Where(x => x.CategoryID == productId).ToList();
         foreach (var p in listProducts)
         {
             context.Entry(p).State = EntityState.Deleted;
         }
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }

    public CategoryViewModel GetEditCategory(int? categoryId)
    {
        Category category = context.Categories.Find(categoryId);
        var categoryViewModel = new CategoryViewModel();
        categoryViewModel.CategoryId = category.CategoryID;
        categoryViewModel.CategoryName = category.CategoryName;
        categoryViewModel.IsActive = category.IsActive;
        return categoryViewModel;          
    }

    public void PostEditCategory(CategoryViewModel model)
    {
        Category category = context.Categories.Find(model.CategoryId);
        category.CategoryID = model.CategoryId;
        category.CategoryName = model.CategoryName;
        category.IsActive = model.IsActive;
        context.SaveChanges();
    }

    public List<Category> GetCategories()
    {
        context.Configuration.ProxyCreationEnabled = false;
        return context.Categories.OrderBy(a => a.CategoryName).ToList();
    }

    public Category GetCategoryById(int? categoryId)
    {
        return context.Categories.Find(categoryId);
    }

这是类别存储库类

我只是想知道为什么会出现异常以及我应该做什么。

最佳答案

这是我尝试过的答案,它正在工作。 https://stackoverflow.com/a/45052060/12061392 这个答案最好有最好的解释

关于asp.net-mvc - 如何修复 "Exception of type ' Unity.Exceptions.InvalidRegistrationException' 被抛出”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58586679/

相关文章:

c# - ASP NET Core 返回文件使用 IE11 失败?内容长度不匹配

asp.net-mvc - ASP.Net MVC ActionFilterAttributes 排序全局与本地

c# - ASP.NET MVC 3 Razor 性能

javascript - 为什么在 asp.net mvc 中更改图像链接会异步调用 Controller

asp.net-mvc - 设置 ASP.NET MVC ControllerFactory 无效

c# - HttpWebRequest.Create 返回 (500) 内部服务器错误

c# - 如何让 MVC 接受参数中的点?

asp.net - 有关使用字段子集的 View 模型和模型更新的最佳实践

javascript - ASP.Net MVC 中的 Excel 文件

asp.net - MVC 4 数据注释 "Display"属性