如何从母版页中动态列出现有的操作控制器?

发布于 2024-09-19 02:19:06 字数 331 浏览 5 评论 0原文

我想列出母版页中的所有现有控制器。

例如:

在我的 Controller 文件夹中,我有:

  1. HomeController ,带有操作 Index 等...
  2. ProductController 带有操作 Index、Details 等...
  3. ServiceController 带有操作 Index、Edit 等...
  4. SomethingController 带有操作 Index、Update 等。 ..

在我的 Site.master 中,我想动态列出所有这些操作控制器。

I want to list all the existing controllers from within master page.

For example:

In my Controller folder I have:

  1. HomeController with actions Index, etc...
  2. ProductController with actions Index, Details, etc...
  3. ServiceController with actions Index, Edit, etc...
  4. SomethingController with actions Index, Update, etc...
  5. etc

From within my Site.master, I want to list all of these Action controller dynamically.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷弦 2024-09-26 02:19:06

虽然我完全同意@Darin的回复,这是一种相对简单的方法来完成您所要求的操作,使用 ViewData。

关于枚举所有控制器的实际逻辑。我不会在这里添加,因为这是一个合理的这样做的方式,我可能无法改进。

您可以使用 ViewData 来执行一些操作,从 OnActionExecuting 动态存储所有控制器和操作。您必须创建一个新控制器,所有其他控制器都将从该控制器继承。

因此,例如,使用您的 ProductController:

public class ProductController : MyCustomController
{
  ...
}

...

public class MyCustomController : Controller
{
  protected override void OnActionExecuting(ActionExecutingContext filterContext)
  {
    base.OnActionExecuting(filterContext);

    //Some logic (not written) to reflect each of the controllers and actions. Probably something utilising IGrouping.
    var controllersActions = GetControllersActions();

    //Drop your collection into the ControllersActions.
    filterContext.Controller.ViewData.Add("ControllersActions", controllersActions);
  }
}

在您的母版页中...

<% var controllersActions = ViewData["ControllersActions"]; %>

... outputting your controllers/actions ...

Whilst I totally agree with @Darin's response, here is a relatively simple way to do what you're asking, using ViewData.

Regarding the actual logic for enumerating all controllers. I won't add that here as this is a reasonable way of doing it, that I probably couldn't improve upon.

You could do something using ViewData to store all of your controllers and actions dynamically from OnActionExecuting. You'd have to create a new controller from which all of your other controllers would inherit.

So, for example, with your ProductController:

public class ProductController : MyCustomController
{
  ...
}

...

public class MyCustomController : Controller
{
  protected override void OnActionExecuting(ActionExecutingContext filterContext)
  {
    base.OnActionExecuting(filterContext);

    //Some logic (not written) to reflect each of the controllers and actions. Probably something utilising IGrouping.
    var controllersActions = GetControllersActions();

    //Drop your collection into the ControllersActions.
    filterContext.Controller.ViewData.Add("ControllersActions", controllersActions);
  }
}

In your master page...

<% var controllersActions = ViewData["ControllersActions"]; %>

... outputting your controllers/actions ...
错々过的事 2024-09-26 02:19:06

在我的 Site.master 中,我想动态列出所有这些 Action 控制器。

视图不应该提取数据。它们应该显示控制器传递的数据/模型。因此,在控制器内部,您可以使用反射来列出从控制器派生的所有类型,并将它们传递给视图进行渲染。另一种可能性是使用 HTML 帮助器来完成这项工作。

From within my Site.master, I want to list all of these Action controller dynamically.

Views are not supposed to pull data. They are supposed to show data/model passed by a controller. So inside your controller you could use reflection to list all the types deriving from Controller and pass them to the view for rendering. Another possibility is to have an HTML helper that will do the job.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文