How can I fix this?

Davi Leao Gomes 20 Reputation points
2023-05-16T14:55:27.2266667+00:00

I am getting this error message on the Error List, but when I look up the code, I get no error highlight (red squiggly line), so there's no tips on how to fix this. The error is "An object reference is required for the non-static field, method, or property 'Mapper.Map<List<User>, List<UserViewModel>>(List<User>)'".
I don't understand the code fully (I didn't write it, I'm just making somes changes), but for what I know, it uses MVC, but it uses AutoMapper instead of Entity Framework.
The code is the following:

using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Security;
using App.Infra;
using App.Model;
using AutoMapper;
using Domain.Entities;
using Domain.IApp;
using Domain.ValueObject;
using System.Globalization;

namespace Web.Controllers
{
    public class LoginController : BaseController
    {
        // GET: Login

        private readonly IUserApp _userApp;
        private readonly IFunctionApp _functionApp;
        private readonly IChannelApp _channelApp;

        public LoginController(IUserApp userApp, IFunctionApp functionApp, IChannelApp channelApp)
        {
            _userApp = userApp;
            _functionApp = functionApp;
            _channelApp = channelApp;
        }

        public ActionResult LogOn()
        {

            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LoginViewModel login)
        {

            var loginAd = ValidateLogin.ActiveDirectoryAuthenticate(login.User, login.Password);
            User userAd = null;

            if (loginAd != null)
            {
                userAd = _userApp.GetUserBySId(loginAd.AdUserId);

                if (userAd == null)
                {
                    var u = new UserViewModel()
                    {
                        AdUserId = loginAd.AdUserId,
                        Name = loginAd.Name,
                        Mail = loginAd.Mail,
                        DtInclude = DateTime.Now,
                        Perfil = loginAd.Perfil,
                        Login = login.User
                    };

                    var user = Mapper.Map<UserViewModel, User>(u);
                    _userApp.Add(user);


                    var function = Mapper.Map<FunctionViewModel, Function>(ValidateLogin.Permissions(user.Id, user.Perfil));

                    _functionApp.Add(function);

                }
                else
                {
                    var functionByUserId = _functionApp.GetByUserId(userAd.Id);


                    if (functionByUserId != null)
                    {
                        var perfil = ValidateLogin.ValidateGroup(login.User, "Bandeirantes");

                        //  var perfil = Perfil.APP_REC_ADMIN_PORTAL;

                        bool updatePerfil = false;
                        if (userAd.DtUpdate < Convert.ToDateTime("05/11/2016 10:29:27", new CultureInfo("pt-BR")))
                        {
                            updatePerfil = true;
                        }

                        if (perfil != userAd.Perfil || updatePerfil)
                        {

                            userAd.Perfil = (Perfil)perfil;

                            var functionValidate = _functionApp.GetByUserId(userAd.Id);

                            IList<int> list = new List<int>();
                            foreach (var item in functionValidate.Channels)
                            {
                                list.Add(item.Id);
                            }

                            foreach (var item in list)
                            {
                                _functionApp.RemoveRelation(functionValidate.Id, item);
                            }

                            var function = ValidateLogin.Permissions(userAd.Id, userAd.Perfil);


                            foreach (var item in function.Channels)
                            {
                                _functionApp.UpdateRelation(functionValidate.Id, item.Id);
                            }

                        }
                        else
                        {
                            if (userAd.Perfil == Perfil.APP_REC_ADMIN_PORTAL || userAd.Perfil == Perfil.APP_REC_PORTAL)
                            {
                                var functionValidate = _functionApp.GetByUserId(userAd.Id);
                                functionValidate.PublishYT = true;
                                functionValidate.PublishUOL = true;

                                _functionApp.Update(functionValidate);

                            }
                        }


                    }
                    else if (functionByUserId == null)
                    {

                        var function = ValidateLogin.Permissions(userAd.Id, userAd.Perfil);

                        var domain = new Function();
                        domain.PublishUOL = function.PublishUOL;
                        domain.PublishYT = function.PublishYT;
                        domain.DownloadVideo = function.DownloadVideo;
                        domain.UserId = userAd.Id;
                        domain.DtInclude = DateTime.Now;
                        _functionApp.Add(domain);

                        foreach (var item in function.Channels)
                        {
                            _functionApp.UpdateRelation(domain.Id, item.Id);
                        }

                    }

                    userAd.DtUpdate = DateTime.Now;
                    _userApp.Update(userAd);
                }


                FormsAuthentication.SetAuthCookie(loginAd.Name + "|" + loginAd.AdUserId + "|" + userAd.Perfil + "|" + loginAd.Mail.Adress, true);

                return RedirectToAction("Index", "Home");
            }

            Danger("Usuário ou senha incorreto!");
            return View();
        }

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return View("LogOn");
        }


    }
}
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,363 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,484 questions
0 comments No comments
{count} votes

Accepted answer
  1. QiYou-MSFT 4,311 Reputation points Microsoft Vendor
    2023-05-17T07:13:19.7633333+00:00

    Hi @Davi Leao Gomes

    First, let's start with the reported error: "An object reference is required for the non-static field, method, or property"

    The problem is that your method is not static, but you use it without instantiation.

    Test1

    Test2

    I looked at Mapper's Map method and it doesn't seem to be static.

    Test3

    Actually, this has to do with the version of AutoMapper you're using. Starting with 11.0, it does not support the .NET Framework. As of 9.0, it has no static API support.

    You can learn more from:

    Configuration

    9.0 Upgrade Guide

    11.0 Upgrade Guide

    Best Regards

    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Ibrahim Refaat 0 Reputation points
    2023-06-13T18:12:04.4833333+00:00

    How can I fix it ?

    0 comments No comments