控制器作为 ascx 工厂 - 坏主意?

发布于 2024-10-01 15:37:49 字数 782 浏览 0 评论 0原文

我正在尝试创建类似 *.ascxs' 工厂的东西。

设想: 我想渲染取决于模型的控件,我已将其传递给partialView。 我想实现这样的目标:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyAbstractModel>" %>

    <%= Model.Property1 %>
<!-- other more sophisticated displays on model -->

    <% Html.RenderAction("RenderControl", "Factory", new { model = Model});  %>

FactoryController:

public ActionResult RenderControl(object model) {
    if (model.GetType() == typeof(Model1) {
        return RenderPartial("Partial2", model);
    } else {
        return RenderPartial("Partial1", model);
    }
}

我想知道是否有更好的方法来应对这种情况。我想这不是在 ASP.MVC 2 中构建网页的最有效方法。

如果这种方法可以接受,我如何限制对此类控制器的访问?我想仅在服务器端且仅通过 ascxs 页面使用此类

I'm trying to create something like *.ascxs' factory.

Scenario:
I would like to render controls which depends on model, which i've passed to partialView.
I'd like to achieve something like this:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyAbstractModel>" %>

    <%= Model.Property1 %>
<!-- other more sophisticated displays on model -->

    <% Html.RenderAction("RenderControl", "Factory", new { model = Model});  %>

FactoryController:

public ActionResult RenderControl(object model) {
    if (model.GetType() == typeof(Model1) {
        return RenderPartial("Partial2", model);
    } else {
        return RenderPartial("Partial1", model);
    }
}

I'd like to know is there any better way to cope with such situation. I suppose It's not the most efficient method to build web page in ASP.MVC 2.

If this method is acceptable, how can i restrict access to such controller? I would like to use this class only on server side and only by ascxs' pages

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

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

发布评论

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

评论(1

落在眉间の轻吻 2024-10-08 15:37:49

使用 ChildActionOnly() 属性来限制对操作的访问。

您想要做的事情已经内置到 MVC 中:Html.DisplayFor()

请参阅:http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html

Use the ChildActionOnly() attribute to restrict access to your actions.

What you are trying to do is already builtin to MVC: Html.DisplayFor()

See: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html

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