ASP.NET MVC:沒有為此對象定義的無參數構造函數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: Line 16: HttpContext.Current.RewritePath(Request.ApplicationPath, false);Line 17: IHttpHandler httpHandler = new MvcHttpHandler();Line 18: httpHandler.ProcessRequest(HttpContext.Current);Line 19: HttpContext.Current.RewritePath(originalPath, false);Line 20: }我正在關注Steven Sanderson的“ Pro ASP.NET MVC框架 ”一書。在頁132,根據作者的建議,我下載了ASP.NET MVC Futures程序集,并將其添加到我的MVC項目中。[注意:這可能是一個紅鯡魚。]在此之后,我無法再加載我的項目。上面的錯誤阻止了我。我的問題不是,“你能幫我解決一下我的代碼嗎?”相反,我想更全面地了解:我該如何解決這個問題?我應該尋找什么?根本原因是什么?看起來我應該比現在更深入地了解路由和控制器。
3 回答
慕勒3428872
TA貢獻1848條經驗 獲得超6個贊
我剛遇到類似的問題。當a Model沒有無參數構造函數時,會發生相同的異常。
調用堆棧正在計算一個負責創建模型新實例的方法。
System.Web.Mvc.DefaultModelBinder。CreateModel(ControllerContext controllerContext,ModelBindingContext bindingContext,Type modelType)
這是一個示例:
public class MyController : Controller{
public ActionResult Action(MyModel model)
{
}}public class MyModel{
public MyModel(IHelper helper) // MVC cannot call that
{
// ...
}
public MyModel() // MVC can call that
{
}}
牧羊人nacy
TA貢獻1862條經驗 獲得超7個贊
您需要與控制器對應的操作沒有參數。
看起來你有控制器/動作組合:
public ActionResult Action(int parameter){}但你需要
public ActionResult Action(){}另外,查看Phil Haack的Route Debugger來排除路由故障。
添加回答
舉報
0/150
提交
取消
