相同的代码在其他项目中工作,但在这里它将给出此错误如何解决错误。
下面提到这个错误。
'/'应用程序中的服务器错误。
否为此对象定义的无参数构造函数。
描述:在执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.MissingMethodException:没有为此对象定义无参数构造函数。
源错误:
在执行当前Web请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息。
堆栈跟踪:
[MissingMethodException:没有为此对象定义无参数构造函数。]
System.RuntimeTypeHandle.CreateInstance(RuntimeType类型,Boolean publicOnly,Boolean noCheck,Boolean& canBeCached ,RuntimeMethodHandleInternal& ctor,Boolean& bNeedSecurityCheck)+0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly,Boolean skipCheckThis,Boolean fillCache,StackCrawlMark& stackMark)+113
系统。 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(Request Context requestContext,Type controllerType)+55
[InvalidOperationException:尝试创建'SAIJobs.Web.Controllers.AccountController'类型的控制器时发生错误。确保控制器具有无参数的公共构造函数。]
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext,Type controllerType)+177
System.Web。 Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext,Type controllerType)+77
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext,String controllerName)+88
系统。 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回调,对象状态)+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,布尔和放大器;已完成异步)+155
版本信息:Microsoft .NET Framework版本:4.0.30319; ASP.NET版本:4.0.30319.34209
我尝试过:
使用SAIJobs.Web.Models;
使用System.Linq;
使用System.Threading.Tasks;
使用System.Web。 Mvc;
使用SAIJobs.Core.Identity;
使用SAIJobs.Core.DomainModels.Identity;
使用System.Net.Mail;
使用SAIJobs.Web.Common;
使用System.Drawing;
使用System;
使用System.Security .Cryptography;
使用System.Web.Security;
使用Microsoft.AspNet.Identity;
使用System.Collections.Generic;
使用System.Net;
使用System.IO;
名称空间SAIJobs.Web.Controllers
{
[OutputCache(NoStore = true,Duration = 0,VaryByParam =None)]
公共类AccountController:Controller
{
私有IApplicationUserManager _userManager;
public AccountController(IApplicationUserManager userManager)
{_userManager = userManager; }
[AllowAnonymous]
public ActionResult Register()
{
string salt = CreateSalt(saltSize);
会话[盐] =盐;
返回查看();
}
// POST:/ Account / Register
[HttpPost,AllowAnonymous,ValidateAntiForgeryToken]
公共异步任务< actionresult>注册(RegisterViewModel模型)
{
string salt = CreateSalt(saltSize);
尝试
{
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);
模式l.Hid2 = AESEncrytDecry.DecryptStringAES(enycnfrpass);
if((saltval!= fistPass.Substring(0,24))|| (saltval!= cnfrPass.Substring(0,24)))
{
ModelState.AddModelError(,失败再试一次。);
返回查看(型号);
}
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,客户);
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 =请点击以下链接确认您的帐户:链接 ;
等待_userManager.SendEmailAsync(user.Id,确认你的账户,msg);
ViewBag.Link = callbackUrl;
var domainname = model.Email.Split('@'); ;
ViewBag.link1 =域名[1];
返回查看(DisplayEmail);
}
AddErrors(结果);
}
}
else
{
Session [salt] = salt;
model.Captcha = null;
ModelState.AddModelError(,Captcha Code is Incorrect。);
返回查看(型号);
}
}
catch(例外e)
{
会话[盐] =盐;
model.Captcha = null;
ModelState.AddModelError(,失败尝试再次。);
//如果我们到目前为止,有些事情失败,重新显示形式
返回查看(型号);
}
会话[盐] =盐;
型号。 Captcha = null;
ModelState.AddModelError(,失败再试一次。);
//如果我们到目前为止,有些事情失败,重新显示形式
返回查看(模型);
}
}
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);
}
}
尝试创建类型为 SAIJobs.Web.Controllers.AccountController
的控制器时发生错误>。确保控制器具有无参数的公共构造函数。
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.
该控制器上唯一的构造函数需要一个类型为 IApplicationUserManager
的参数。
ASP.NET MVC 5中似乎添加了对控制器构造函数中依赖注入的内置支持,因此请确保您的项目未使用早期版本。如果是,您将需要一个自定义控制器工厂。
此外,请确保您已正确配置DI容器。
ASP.NET MVC中的依赖注入 - 简介| DotNetCurry [ ^ ]
阅读错误消息:
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.