构造函数如何在ASP.NET MVC控制器内部工作 [英] How the constructors work internally for ASP.NET MVC controllers

查看:87
本文介绍了构造函数如何在ASP.NET MVC控制器内部工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在过去的3-4个月里一直在研究Asp.Net MVC并且在脑海里有一个问题,这让我感到很困惑我支付b $ b

我在互联网上搜索了同样的内容,但却什么也没找到。



问题:

我们都知道MVC将按照URL工作 - >首先,它将查找控制器,然后查找指定的操作和参数。

但是,任何用户定义的控制器都是一个类,它继承自基类Controller。

现在,如果我在控制器中声明参数化构造函数。如何调用它?



示例:



我的尝试:



public MyController:Controller

{

public int index;



public MyController(int i)

{

index = 10; < br $>
}



//行动

公共ActionResult someAction()

{< br $>
//实施

}

}



现在,如何调用参数化控制器的构造函数?

解决方案

我不确定我是否关注你。但是为什么你甚至想要一个带有那个定义参数的控制器呢?



这不是跟随URL的控制器,而是执行此操作的操作。您的控制器 - 如果确实需要 - 可以保存成员字段以保存这些值,并且可以在操作中访问它们。



此外,您可以自由地接受路由中的占位符变量。我会以不同的方式进行相同的编码,这似乎更容易和直接。

  public  MyController:Controller 
{
public int index;

public MyController()
{
}

// 行动
[路线( controller / {i})]
public ActionResult someAction( int i)
{
// 实施
}
}



最后,ASP.NET确实支持参数,了解它们如何工作,你可以了解更多有关依赖注入的知识,以了解框架将如何注入构造函数的参数值。 ASP.NET MVC 4依赖注入| Microsoft Docs [ ^ ]

.net - 如何设置MVC控制器的构造函数参数? - 软件工程堆栈交换 [ ^ ]


MVC使用控制器工厂来创建控制器。显然,它不知道传递构造函数的值或从何处获取它,因此它只允许在没有无参数构造函数的情况下创建控制器。要允许带有参数的控制器,您必须自己编写工厂并自己创建构造函数,从需要读取的位置读取值并自行传递。



了解和扩展MVC中的控制器工厂 [ ^ ]



你可能不希望这样做,但是,可能有一些其他的解决方案,但除非我们知道它的价值是什么以及它来自哪里很难说

Hi All,

I have been working on Asp.Net MVC from past 3-4 months and got a question in my mind, which is really puzzling me out.

I have searched the same in the internet but came up with nothing.

Problem:
We all know that MVC will work as per the URL -> first it will look for the controller and then the specified action and parameter(s).
But, any user-defined controller is a class and it inherits from the base class "Controller".
Now, if I declare a parameterized constructor in my controller. How to invoke it?

Example:

What I have tried:

public MyController : Controller
{
public int index;

public MyController(int i)
{
index = 10;
}

// Action
public ActionResult someAction()
{
// implementation
}
}

Now, how to call the parameterized constructor of the controller?

解决方案

I am not sure whether I am following you or not. But why do you even want to have a controller with that defined parameter?

It is not the controller that follows the URL, but instead the actions that do so. Your controller — if really needed — can hold member fields to hold those values, and they can be accessed in the action.

Also, you have the freedom to accept the placeholder variables in routing. I would do the same coding in the different manner, this seems somewhat easier and straightforward.

public MyController : Controller
{
   public int index;

   public MyController()
   {
   }

   // Action
   [Route("controller/{i}")]
   public ActionResult someAction(int i)
   {
      // implementation
   }
}


Lastly, ASP.NET does support parameters, to understand how they work you can learn more about Dependency Injection to learn how framework would inject values for the parameters of a constructor. ASP.NET MVC 4 Dependency Injection | Microsoft Docs[^]
.net - How do constructor parameters of a MVC Controller get set? - Software Engineering Stack Exchange[^]


MVC uses a controller factory to create your controllers. Obviously it doesn't know what value to pass your constructor or where to get it from, so it only allows controllers to be created if they have parameterless constructors. To allow for controllers with params you'd have to write your own factory and create the constructor yourself, reading the value from where it needs to be read from and passing it yourself.

Understanding and Extending Controller Factory in MVC[^]

Chances are you don't want\need to do that however, there is probably some other solution but unless we know what the value is for and where it comes from it's hard to say.


这篇关于构造函数如何在ASP.NET MVC控制器内部工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆