0

为什么当我为控制器的构造函数添加参数时,出现错误。

这是我的代码:

public partial class RegistrationController : Controller
    {

        public RegistrationController(int i)
        {


            }
 public ActionResult Index()
    {}

}
the error is `Server Error in '/' Application.
Object reference not set to an instance of an 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.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 14: 
Line 15:     <fieldset>
Line 16:         @{string visibleCountry = (Model.CountryParam == ConfigurationParamValue.NotVisible) ? "display:none" : "display:inline";
Line 17:           string mandatoryCountry = (Model.CountryParam == ConfigurationParamValue.isMandatory) ? "display:inline" : "display:none";  
Line 18:           <div id="county" style="@visibleCountry">


Source File: e:\VP5-Nuguet\src\Registration\Registration.Front.Web\Views\Registration\Index.cshtml    Line: 16

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   ASP._Page_Views_Registration_Index_cshtml.Execute() in e:\VP5-Nuguet\src\Registration\Registration.Front.Web\Views\Registration\Index.cshtml:16
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +97
   System.Web.WebPages.StartPage.RunPage() +17
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +62
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +260
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +295
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +23
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +242
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +21
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +89
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9630364
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929 `
4

2 回答 2

0

MVC 将尝试通过调用其默认构造函数来实例化每个请求的路由中指定的控制器。

从它的外观上看,你想点击一个类似的 URL:/registration/{someNumber}

如果是这种情况,请将int id构造函数中的 移动到操作方法:

public partial class RegistrationController : Controller
{
        public RegistrationController()
        { }

        public ActionResult Index(int i)
        { 
          // do something
        }
}
于 2013-07-24T15:56:08.410 回答
0

如果您坚持使用构造函数参数,则必须构建自己的 ControllerFactory 实现IControllerFactory接口并在应用程序启动时进行设置。

ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());
于 2013-07-25T09:04:53.497 回答