C# “/”应用程序中的服务器错误。没有为此对象定义无参数构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/331276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Server Error in '/' Application. No parameterless constructor defined for this object
提问by Boris Callens
The callstack shows the following:
调用堆栈显示以下内容:
[MissingMethodException: No parameterless constructor defined for this object.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache) +86
System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache) +230
System.Activator.CreateInstance(Type type, Boolean nonPublic) +67
System.Activator.CreateInstance(Type type) +6
System.Web.Mvc.DefaultModelBinder.CreateModel(ModelBindingContext bindingContext, Type modelType) +277
System.Web.Mvc.<>c__DisplayClass1.<BindModel>b__0() +98
System.Web.Mvc.ModelBindingContext.get_Model() +51
System.Web.Mvc.DefaultModelBinder.BindModelCore(ModelBindingContext bindingContext) +2600
System.Web.Mvc.DefaultModelBinder.BindModel(ModelBindingContext bindingContext) +1067
System.Web.Mvc.DefaultModelBinder.BindProperty(ModelBindingContext parentContext, Type propertyType, Func`1 propertyValueProvider, String propertyName) +208
System.Web.Mvc.DefaultModelBinder.BindModelCore(ModelBindingContext bindingContext) +1787
System.Web.Mvc.DefaultModelBinder.BindModel(ModelBindingContext bindingContext) +1067
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ParameterInfo parameterInfo) +355
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(MethodInfo methodInfo) +439
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +288
System.Web.Mvc.Controller.ExecuteCore() +180
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +96
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +36
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +377
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +71
System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +36
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
I have a tiny form with a bunch of hidden fields and one submit button. When I press it, I never even hit the requested method.
我有一个带有一堆隐藏字段和一个提交按钮的小表单。当我按下它时,我什至从未点击请求的方法。
How do I go on and debug this? It would be a great start if I knew WHAT object didn't have a parameterless constructor. Where is this object? How can I solve this? I know the question is rather vague, but currently it's all I've got..
我该如何继续并调试它?如果我知道 WHAT 对象没有无参数构造函数,那将是一个很好的开始。这个对象在哪里?我该如何解决这个问题?我知道这个问题相当含糊,但目前我只有..
--EDIT--
In my form I added Html.Hidden() inputs. Depending on previous actions, these can have a value of "". The action makes use of ModelBinding. Whenever the value is "" and the datatype is a SelectList, the modelbinder goes berzerk on me.
--EDIT--
在我的表单中,我添加了 Html.Hidden() 输入。根据之前的操作,这些可以具有值“”。该操作使用 ModelBinding。每当值是 "" 并且数据类型是 SelectList 时,模型绑定器就会对我发疯。
I feel more and more uncomfortable with how the SelectList is doing it's thing... The idea is good, but there are some issues with it.
我对 SelectList 的工作方式越来越感到不舒服......这个想法很好,但它有一些问题。
采纳答案by Gripsoft
I solved the issue which is caused of SelectList object because it does not provide the default constructor so we cant have in out ViewModel.
我解决了由 SelectList 对象引起的问题,因为它不提供默认构造函数,因此我们无法输入 ViewModel。
回答by Boris Callens
Did you remove Default.aspx? That needs to be in place to render the root properly. This can also happen if you're trying to use Dependency Injection and your container isn't setup properly - meaning there is no ControllerFactory that asks the DI Container for the instance.
您是否删除了 Default.aspx?这需要到位才能正确渲染根。如果您尝试使用依赖注入并且您的容器未正确设置,也可能发生这种情况 - 这意味着没有 ControllerFactory 向 DI 容器询问实例。
回答by Craig Stuntz
Your custom classes in the action args must have a parameterless constructor in order to use the default model binder. Otherwise, either (1) create a custom model binder or (2) pass primitive types instead of custom classes to the Action.
您在操作 args 中的自定义类必须具有无参数构造函数才能使用默认模型绑定器。否则,要么 (1) 创建自定义模型绑定器,要么 (2) 将原始类型而不是自定义类传递给 Action。
回答by Gripsoft
I also have properties on my view model class that return SelectList instances. I decorated my class with the Bind attribute to exclude these properties like this:
我的视图模型类也有返回 SelectList 实例的属性。我用 Bind 属性装饰了我的类以排除这些属性,如下所示:
[Bind(Exclude = "Countries")]
public class MyViewModel
{
...
[Bind(Exclude = "Countries")]
public class MyViewModel
{
...
public SelectList Countries { get; set; }
public SelectList Countries { get; set; }
}
}
Hope that helps, Roger
希望有帮助,罗杰
回答by jbierling
The root of my problem was also SelectList.
我的问题的根源也是 SelectList。
I had this:
我有这个:
<%: Html.DropDownListFor(m => Model.Destinations, Model.Destinations)%>
When I should have had this:
当我应该有这个的时候:
<%: Html.DropDownListFor(m => Model.Destination, Model.Destinations)%>
Destination is the user's selection, Destinations is the list of possible values. Using the plural SelectList twice caused the problem.
Destination 是用户的选择,Destination 是可能值的列表。两次使用复数 SelectList 导致了问题。
回答by Josh Mouch
For those Googling this exception, here is a more general way to diagnose it:
对于那些在谷歌上搜索这个异常的人,这里有一个更通用的诊断方法:
The only easy way I've found to diagnose this problem is to override MVC as close to the exception as possible with your own code. Then your code will break inside Visual Studio when this exception occurs, and you can read the Type causing the problem from the stack trace.
我发现诊断此问题的唯一简单方法是使用您自己的代码覆盖尽可能接近异常的 MVC。然后,当发生此异常时,您的代码将在 Visual Studio 中中断,您可以从堆栈跟踪中读取导致问题的类型。
This seems like a horrible way to approach this problem, but it's very fast, and very consistent.
这似乎是解决这个问题的可怕方法,但它非常快,而且非常一致。
For example, if this error is occurring inside the MVC DefaultModelBinder (which you will know by checking the stack trace), then replace the DefaultModelBinder with this code:
例如,如果此错误发生在 MVC DefaultModelBinder 内部(您将通过检查堆栈跟踪知道),然后将 DefaultModelBinder 替换为以下代码:
public class MyDefaultModelBinder : System.Web.Mvc.DefaultModelBinder
{
protected override object CreateModel(System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext, Type modelType)
{
return base.CreateModel(controllerContext, bindingContext, modelType);
}
}
And update your Global.asax.cs:
并更新您的 Global.asax.cs:
public class MvcApplication : System.Web.HttpApplication
{
...
protected void Application_Start(object sender, EventArgs e)
{
ModelBinders.Binders.DefaultBinder = new MyDefaultModelBinder();
}
}
Now the next time you get that exception, Visual Studio will stop inside your MyDefaultModelBinder class, and you can check the "modelType" property to see what type caused the problem.
现在,下次您遇到该异常时,Visual Studio 将停止在您的 MyDefaultModelBinder 类中,您可以检查“modelType”属性以查看导致问题的类型。
The example above works for when you get the "No parameterless constructor defined for this object" exception during model binding, only. But similar code can be written for other extension points in MVC (e.g. controller construction).
上面的示例仅适用于在模型绑定期间获得“没有为此对象定义无参数构造函数”异常的情况。但是可以为 MVC 中的其他扩展点编写类似的代码(例如控制器构造)。