如何为没有控制器的视图配置 Fubu?
我在控制器上有一个没有执行任何操作的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 FubuRegistry 的“Views”部分中,添加:
.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:
Note that you can both ASPX and ASCX for headless views. If you only want ASCX files, then you can use
WebFormViewFacility.IsWebFormControl
instead.对我有用:
Works for me: