如何为没有控制器的视图配置 Fubu?

发布于 2024-10-20 07:19:40 字数 2899 浏览 1 评论 0原文

我在控制器上有一个没有执行任何操作的 Index 操作。

public EmptyModel Index()
{
     return null;
}

Index 视图仅显示一些 html,并使用 jQuery 驱动的 ajax 和 MasterPage 完成此特定页面上的所有繁重工作。当我从控制器中删除此操作函数时,aspx 视图将不再显示。


更多信息和更新:

在进行 Chad 答案中提到的更改后,用于返回索引视图的 url 现在返回 404。此问题可能存在,因为大多数视图的文件夹结构是在早期 Fubu Framework 风格中完成的(使用View_Page_Type_Declarations.cs 并且没有代码隐藏),而不是使用更直观和更新的默认文件夹约定。但我的分析也有可能是错误的。

这是我的 FubuRegistry:

public WebAppFubuRegistry()
{
    IncludeDiagnostics(true);

    Services(x => x.SetServiceIfNone<IWebAppSecurityContext, WebAppSecurityContext>());

    Applies.ToThisAssembly()
        .ToAssemblyContainingType<HomeController>();


    Actions
        .IncludeClassesSuffixedWithController();


    Routes
        .UrlPolicy<WebAppUrlPolicy>()
        .IgnoreControllerNamespaceEntirely()
        .ConstrainToHttpMethod(action => action.Method.Name.StartsWith("Perform"), "POST");


    Views
        .TryToAttach(x=> x.by<ViewAndActionInDifferentFolders>())
        .TryToAttachWithDefaultConventions()
        .RegisterActionLessViews(WebFormViewFacility.IsWebFormView,
               chain => chain.PartialOnly());

    /*Behavior Code */
}

WebAppUrlPolicy:

public class WebAppUrlPolicy : IUrlPolicy
    {
        public bool Matches(ActionCall call, IConfigurationObserver log)
        {
            return true;
        }

        public IRouteDefinition Build(ActionCall call)
        {
            if(call.IsForHomeController())
                return new RouteDefinition("home");

            if(call.IsAnIndexCall())
                return new RouteDefinition(call.ControllerPrefix());

            var otherRoute = new RouteDefinition(call.ToControllerActionRoute());

            return otherRoute;
        }
    }

ViewAndActionInDifferentFolders:

public class ViewAndActionInDifferentFolders : IViewsForActionFilter
    {
        public IEnumerable<IViewToken> Apply(ActionCall call, ViewBag views)
        {
            if (call.IsForHomeController())
            {
                var viewTokens = views.ViewsFor(call.OutputType()).Where(x => x.Name == "HomeIndexView");
                return new[] { new WebAppViewToken(call, viewTokens, "home") };
            }
            if (call.IsJsonCall())
            {
                return new List<IViewToken>();
            }
            return CreateSingleTokenList(call, views);
        }
        private static IEnumerable<WebAppViewToken> CreateSingleTokenList(ActionCall call, ViewBag views)
        {
            return new[] { new WebAppViewToken(call, views.ViewsFor(call.OutputType())) };
        }
    }

如何重新配置​​ Fubu 以便我可以使用没有操作的视图?

需要进行哪些更改才能删除上面的操作功能,并仍然保持相同的功能?

I have an Index action on a controller that's not doing anything.

public EmptyModel Index()
{
     return null;
}

The Index view simply displays some html, with jQuery-driven ajax and the MasterPage doing all the heavy lifting on this particular page. When I remove this action function from it's controller, the aspx view will no longer display.


More Information and Update:

After making the changes mentioned in Chad's answer the url that used to return the index view now instead returns a 404. This issue may exist because most of the views' folder structure is done in the early Fubu Framework style (with View_Page_Type_Declarations.cs and no code-behinds), rather than using the more intuitive and more recent default folder conventions. But it's possible my analysis is off.

Here's my FubuRegistry:

public WebAppFubuRegistry()
{
    IncludeDiagnostics(true);

    Services(x => x.SetServiceIfNone<IWebAppSecurityContext, WebAppSecurityContext>());

    Applies.ToThisAssembly()
        .ToAssemblyContainingType<HomeController>();


    Actions
        .IncludeClassesSuffixedWithController();


    Routes
        .UrlPolicy<WebAppUrlPolicy>()
        .IgnoreControllerNamespaceEntirely()
        .ConstrainToHttpMethod(action => action.Method.Name.StartsWith("Perform"), "POST");


    Views
        .TryToAttach(x=> x.by<ViewAndActionInDifferentFolders>())
        .TryToAttachWithDefaultConventions()
        .RegisterActionLessViews(WebFormViewFacility.IsWebFormView,
               chain => chain.PartialOnly());

    /*Behavior Code */
}

WebAppUrlPolicy:

public class WebAppUrlPolicy : IUrlPolicy
    {
        public bool Matches(ActionCall call, IConfigurationObserver log)
        {
            return true;
        }

        public IRouteDefinition Build(ActionCall call)
        {
            if(call.IsForHomeController())
                return new RouteDefinition("home");

            if(call.IsAnIndexCall())
                return new RouteDefinition(call.ControllerPrefix());

            var otherRoute = new RouteDefinition(call.ToControllerActionRoute());

            return otherRoute;
        }
    }

ViewAndActionInDifferentFolders:

public class ViewAndActionInDifferentFolders : IViewsForActionFilter
    {
        public IEnumerable<IViewToken> Apply(ActionCall call, ViewBag views)
        {
            if (call.IsForHomeController())
            {
                var viewTokens = views.ViewsFor(call.OutputType()).Where(x => x.Name == "HomeIndexView");
                return new[] { new WebAppViewToken(call, viewTokens, "home") };
            }
            if (call.IsJsonCall())
            {
                return new List<IViewToken>();
            }
            return CreateSingleTokenList(call, views);
        }
        private static IEnumerable<WebAppViewToken> CreateSingleTokenList(ActionCall call, ViewBag views)
        {
            return new[] { new WebAppViewToken(call, views.ViewsFor(call.OutputType())) };
        }
    }

How do I reconfigure Fubu so that I can use a view without the action?

What changes need to be made to remove the action function above, and still maintain the same functionality?

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

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

发布评论

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

评论(2

画尸师 2024-10-27 07:19:40

在 FubuRegistry 的“Views”部分中,添加:

.RegisterActionLessViews(WebFormViewFacility.IsWebFormView, chain => chain.PartialOnly());

例如,整个视图部分可能如下所示

        Views
            .TryToAttachWithDefaultConventions()
            .RegisterActionLessViews(
                                        WebFormViewFacility.IsWebFormView, 
                                        chain => chain.PartialOnly());

:您可以使用 ASPX 和 ASCX 来获取无头视图。如果您只需要 ASCX 文件,则可以使用 WebFormViewFacility.IsWebFormControl 代替。

In your FubuRegistry, in the "Views" section, add:

.RegisterActionLessViews(WebFormViewFacility.IsWebFormView, chain => chain.PartialOnly());

For example, the whole views section may look like:

        Views
            .TryToAttachWithDefaultConventions()
            .RegisterActionLessViews(
                                        WebFormViewFacility.IsWebFormView, 
                                        chain => chain.PartialOnly());

Note that you can both ASPX and ASCX for headless views. If you only want ASCX files, then you can use WebFormViewFacility.IsWebFormControl instead.

何处潇湘 2024-10-27 07:19:40

对我有用:

Views.RegisterActionLessViews(type => type.Name == "StaticView", 
       chain => chain.Route = new RouteDefinition("StaticView"));

Works for me:

Views.RegisterActionLessViews(type => type.Name == "StaticView", 
       chain => chain.Route = new RouteDefinition("StaticView"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文