MVCContrib 可移植区域不适用于 HtmlExtensions、MVC 3

发布于 2024-11-03 06:44:32 字数 713 浏览 6 评论 0原文

我刚刚实现了 MVCContrib 的便携式区域功能,它工作正常。我可以使用以下方法打开它: http://localhost/projectname/portableAreaName,但是如果我使用 HtmlHelper 扩展方法渲染它,这个可移植区域将无法工作,如下所示:

public static void  RenderHtmlWidget(this HtmlHelper Html)
{
        Html.RenderAction("Index", "HtmlWidget", new {area = "HtmlWidget"});
}

并在视图中调用辅助方法,如下所示:

@using Project.Widgets.HtmlWidget;
@{Html.RenderHtmlWidget();}

我收到错误:未找到视图“索引”或其主视图,或者没有视图引擎支持搜索的位置。在可能的位置列表中没有定义 ~/areas/...。 但我可以使用视图中的同一行代码成功渲染我的 HtmlWidget:

@{Html.RenderAction("Index", "HtmlWidget", new { area = "HtmlWidget" });}

我做错了什么以及如何正确使用 HtmlHelper 扩展与 MVCContrib 可移植区域功能?

I just implemented MVCContrib's Portable Area feature and it works fine. I can open it using:
http://localhost/projectname/portableAreaName, but this portable area is not working if i render it using the HtmlHelper extension method like this:

public static void  RenderHtmlWidget(this HtmlHelper Html)
{
        Html.RenderAction("Index", "HtmlWidget", new {area = "HtmlWidget"});
}

And calling the helper method in the view as such:

@using Project.Widgets.HtmlWidget;
@{Html.RenderHtmlWidget();}

I'm getting an error: The view 'Index' or its master was not found or no view engine supports the searched locations. In the possible location list there are no ~/areas/... defined.
But I can render my HtmlWidget successfully with this the same line of code in the view:

@{Html.RenderAction("Index", "HtmlWidget", new { area = "HtmlWidget" });}

What am I doing wrong and how should I use the HtmlHelper extensions correctly with the MVCContrib portable areas feature?

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

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

发布评论

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

评论(1

櫻之舞 2024-11-10 06:44:32

有一些原因可能会导致这种情况。

  • 在使用辅助方法调用可移植区域的调用/父项目中,/Areas/ 文件夹中是否有 Web.config 文件?如果没有,您必须复制在同一项目的 /Views/ 文件夹中找到的 Web.config,并将新副本也放在 /Areas/ 文件夹中。
  • 在可移植区域项目的注册类文件中,在“RegisterArea”方法中调用MapRoute之后,是否调用“RegisterAreaEmbeddedResources();”?
  • 您的可移植区域项目中的每个视图是否都是嵌入式资源而不是内容?在解决方案资源管理器中选择一个视图并按 F4,“构建操作”应设置为“嵌入式资源”,但默认为“内容”
  • 您还需要确保可移植项目和使用项目引用相同的版本MvcContrib,但它们也使用相同版本的 ASP.NET MVC。如果您的区域在多个项目中被引用,每个项目都基于不同版本的 MVC(不太可能,但根据具体情况有可能),则您的区域必须使用消费项目使用的任何版本的 MVC。
  • 我还建议使用 Phil Haack 的 .NET 路由调试器 - 它是您在使用应用程序中引用的单个 DLL 文件,并向 Global.asax.cs 中的 ApplicationStart() 添加一行。这对于确定您的可移植区域是否正确注册到基础项目非常有帮助 - 并帮助您切入正题。

There are a few things that may be causing this.

  • In the calling/parent project where you use the helper method to invoke your portable area, do you have a Web.config file in the /Areas/ folder? If not, you must copy the Web.config found in the /Views/ folder of the same project, and simply place the new copy in the /Areas/ folder as well.
  • In the Registration class file in your portable area project, after you call MapRoute in the "RegisterArea" method, are you calling "RegisterAreaEmbeddedResources();"?
  • Is each view in your portable area project made to be an embedded resource as opposed to content? Select a View in the Solution Explorer and hit F4, "Build Action" should be set to "Embedded Resource", but it defaults to "Content"
  • You also need to make sure that both the Portable project and the consuming project reference the same version of MvcContrib, but that they also utilize the same version of ASP.NET MVC. If your area is referenced in multiple projects, each based off of a different version of MVC (not likely, but possible depending on the situation), your area must use whatever version of MVC the consuming project uses.
  • I'd also suggest using Phil Haack's .NET Routing Debugger - its a single DLL file that you reference in the consuming application and add a single line to your ApplicationStart() in your Global.asax.cs. This becomes incredibly helpful in determining if your portable area is being correctly registered with the base project - and helps you cut to the chase.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文