Click here to Skip to main content
15,867,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Getting this error mention below.


Server Error in '/' Application.
No parameterless constructor defined for this object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[MissingMethodException: No parameterless constructor defined for this object.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +113
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232
System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
System.Activator.CreateInstance(Type type) +66
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +55

[InvalidOperationException: An error occurred when trying to create a controller of type 'SAIJobs.Web.Controllers.AccountController'. Make sure that the controller has a parameterless public constructor.]
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +178
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +77
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +88
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +191
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +50
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34209

What I have tried:

using SAIJobs.Web.Models;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using SAIJobs.Core.Identity;
using SAIJobs.Core.DomainModels.Identity;
using System.Net.Mail;
using SAIJobs.Web.Common;
using System.Drawing;
using System;
using System.Security.Cryptography;
using System.Web.Security;
using Microsoft.AspNet.Identity;
using System.Collections.Generic;
using System.Net;
using System.IO;

namespace SAIJobs.Web.Controllers
{
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
public class AccountController : Controller
{
private IApplicationUserManager _userManager;

public AccountController(IApplicationUserManager userManager)
{ _userManager = userManager; }
[AllowAnonymous]
public ActionResult Register()
{
string salt = CreateSalt(saltSize);
Session["salt"] = salt;
return View();
}

// POST: /Account/Register
[HttpPost, AllowAnonymous, ValidateAntiForgeryToken]
public async Task<actionresult> Register(RegisterViewModel model)
{
string salt = CreateSalt(saltSize);
try
{
string realCaptcha = Session["captcha"].ToString();
if (model.Captcha == realCaptcha)
{
string saltval = Session["salt"].ToString();
Session["salt"] = salt;
model.Captcha = null;

var fistPass = AESEncrytDecry.DecryptStringAES(model.Hid1);
var cnfrPass = AESEncrytDecry.DecryptStringAES(model.Hid2);

var enypass = fistPass.Substring(24);
model.Hid1 = AESEncrytDecry.DecryptStringAES(enypass);

var enycnfrpass = cnfrPass.Substring(24);
model.Hid2 = AESEncrytDecry.DecryptStringAES(enycnfrpass);

if ((saltval != fistPass.Substring(0, 24)) || (saltval != cnfrPass.Substring(0, 24)))
{
ModelState.AddModelError("", "Failed try again.");
return View(model);
}

if (ModelState.IsValid)
{
var _profileController = DependencyResolver.Current.GetService<userprofilecontroller>();
ApplicationUserProfile _profile = new ApplicationUserProfile();
_profile.FName = model.UserName;

var user = new AppUser { UserName = model.Email, Email = model.Email, PhoneNumber = model.PhoneNumber };
var result = await _userManager.CreateAsync(user, model.Hid1);

if (result.Succeeded)
{
_profile.LoginId = user.Id;
await _profileController.Create(_profile);

_userManager.AddToRole(user.Id, "Client");



var code = await _userManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code }, Request.Url == null ? "" : Request.Url.Scheme);
string msg = "Please confirm your account by clicking this link: link";
await _userManager.SendEmailAsync(user.Id, "Confirm your account", msg);

ViewBag.Link = callbackUrl;
var domainname = model.Email.Split('@'); ;
ViewBag.link1 = domainname[1];
return View("DisplayEmail");
}
AddErrors(result);
}
}
else
{
Session["salt"] = salt;
model.Captcha = null;
ModelState.AddModelError("", "Captcha Code is Incorrect.");
return View(model);
}
}
catch (Exception e)
{
Session["salt"] = salt;
model.Captcha = null;
ModelState.AddModelError("", "Failed try again.");
// If we got this far, something failed, redisplay form
return View(model);
}

Session["salt"] = salt;
model.Captcha = null;
ModelState.AddModelError("", "Failed try again.");
// If we got this far, something failed, redisplay form
return View(model);
}
}
Posted
Updated 17-May-19 2:30am

Quote:
An error occurred when trying to create a controller of type 'SAIJobs.Web.Controllers.AccountController'. Make sure that the controller has a parameterless public constructor.

The only constructor on that controller expects a single parameter of type IApplicationUserManager.

Built-in support for dependency injection in controller constructors seems to have been added in ASP.NET MVC 5, so make sure your project isn't using an earlier version. If it is, you'll need a custom controller factory.

Also, make sure you've configured your DI container properly.

Dependency Injection in ASP.NET MVC - An Introduction | DotNetCurry[^]
 
Share this answer
 
Read the error message:
No parameterless constructor defined for this object.

It couldn't be much clearer if it tried: you have tried to construct an object using a constructor with no parameters, and there all defined constructors require at least one parameter.

At a guess, it's this line:
ApplicationUserProfile _profile = new ApplicationUserProfile();
(That being the only direct constructor in your code) - so look at the appropriate class and see what you can find out.
 
Share this answer
 
Comments
Richard Deeming 17-May-19 8:31am    
Based on the inner exception, it's actually the controller's constructor that's causing the problem. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900