【问题标题】:How do I configure Fubu for a view without a controller?如何为没有控制器的视图配置 Fubu?
【发布时间】:2011-07-09 07:40:50
【问题描述】:

我有一个Index 对一个没有做任何事情的控制器的操作。

public EmptyModel Index()
{
     return null;
}

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


更多信息和更新:

在进行 Chad 的回答中提到的更改后,用于返回索引视图的 url 现在返回 404。这个问题可能存在,因为大多数视图的文件夹结构都是在早期的 Fubu 框架样式中完成的(@987654324 @ 并且没有代码隐藏),而不是使用更直观和更新的默认文件夹约定。但有可能我的分析失败了。

这是我的 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 以便我可以在没有操作的情况下使用视图?

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

【问题讨论】:

    标签: fubumvc


    【解决方案1】:

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

    .RegisterActionLessViews(WebFormViewFacility.IsWebFormView, chain =&gt; chain.PartialOnly());

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

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

    请注意,您可以将 ASPX 和 ASCX 都用于无头视图。如果您只想要 ASCX 文件,则可以使用 WebFormViewFacility.IsWebFormControl

    【讨论】:

    • 很抱歉再次打扰您,感谢您提供的所有出色答案。出于某种原因,当我进行更改时,您建议 url 无法再找到视图。我在问题中添加了更多细节,以及一些相关文件
    • 我相信 PartialOnly() 意味着你不能在浏览器中查看它——只能在部分中使用它。
    【解决方案2】:

    为我工作:

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

    【讨论】:

      猜你喜欢
      • 2015-12-18
      • 1970-01-01
      • 2015-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      相关资源
      最近更新 更多