0

为什么当我为控制器的构造函数添加一个参数时,出现错误。控制器的构造函数不采取参数mvc

这是我的代码:

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 ` 
+0

参数的要点是什么? –

+0

还添加了一个不带参数的默认构造函数 – Sankara

+0

另外,阅读路由 –

回答

0

如果你坚持,你必须建立自己的ControllerFactory实现IControllerFactory接口,并设置它在构造函数中的参数应用程序启动。

ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory()); 
0

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

从外观上看,您想要找到如下网址:/registration/{someNumber}

如果是这样的情况下,从构造移动int id的动作方法:

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

     public ActionResult Index(int i) 
     { 
      // do something 
     } 
}