如何将 ASP.NET MVC 用户控件构建到类库中
我正在尝试将一些 .ascx 控件构建到我正在构建的 CMS 插件的类库中。
我的项目类型是典型的 C# 类库,添加了 system.web.mvc 和 system.web.mvc 的引用。 朋友们。
我的问题出现在尝试创建强类型用户控件时。 我的控件如下所示:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<TagCloudWidget.Models.TagCloudWidgetData>" %>
即使我在同一个项目中的命名空间 TagCloudWidget.Models
下有另一个公共类,我似乎无法将 .ascx
文件获取到认识到这一点。 我尝试为命名空间包含 Imports
,但随后它只是抱怨命名空间不存在。 TagCloudData 类文件如下所示:
namespace TagCloudWidget.Models
{
public class TagCloudWidgetData
{
public IList<TagCount> TagCounts { get; set; }
public ContentTypes ContentType;
public string Controller;
public string Action;
public TagCloudWidgetData(IList<TagCount> tagCounts, ContentTypes contentType, string controller, string action)
{
TagCounts = tagCounts;
ContentType = contentType;
Controller = controller;
Action = action;
}
}
}
你能看出我做错了什么吗?
由于下面的建议,我尝试添加代码隐藏文件。 我想我已经更接近找出问题所在了,但我仍然无法找到解决方案。 在项目中,我添加了对 System.Web 和 System.Web.Mvc 的引用。 但是,在代码隐藏文件的 using 部分中,当我键入“using System.”时,自动完成功能甚至不会将 System.Web 或 System.Web.Mvc 显示为可用选项。 就像它们根本不存在于光盘上一样。 其他项目能够包含它们并引用它们就可以了。 我想这就是我的问题的根源,所以我希望一旦我弄清楚了这一点,其余的事情就会迎刃而解。 有想法吗?
I'm trying to build some .ascx controls into a class library for plugins for a CMS I'm building.
My project type is a typical C# class libary with references added for system.web.mvc & friends.
My problem arises in trying to create a strongly-typed user control. My control looks like this:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<TagCloudWidget.Models.TagCloudWidgetData>" %>
Even though I have another public class in the same project under the namespace TagCloudWidget.Models
, I can't seem to get the .ascx
file to recognize this. I've tried including Imports
for the namespace, but then it just complains that the namespace doesn't exist. The TagCloudData class file looks like this:
namespace TagCloudWidget.Models
{
public class TagCloudWidgetData
{
public IList<TagCount> TagCounts { get; set; }
public ContentTypes ContentType;
public string Controller;
public string Action;
public TagCloudWidgetData(IList<TagCount> tagCounts, ContentTypes contentType, string controller, string action)
{
TagCounts = tagCounts;
ContentType = contentType;
Controller = controller;
Action = action;
}
}
}
Can you see what I'm doing wrong?
Since the suggestion below, I've tried adding a code-behind file. I think I'm closer to figuring out what's wrong, but I'm still not able to find a solution. In the project, I've added references to System.Web and System.Web.Mvc. But, in the using section of the codebehind file, when I type "using System.", auto-complete doesn't even show System.Web or System.Web.Mvc as available options. It's like they just don't exist on disc. Other projects are able to include them and reference them just fine. I suppose this is the source of my problem, so I'm hopeful once I figure this out the rest will just fall into place. Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我从测试版开始使用的应用程序也遇到了类似的问题。 我发现唯一有效的方法是为控件创建一个代码隐藏文件并在代码隐藏中定义继承。 然后在控件中,让它继承代码隐藏中定义的类。 在使用 1.0 从头开始构建的较新项目中,我没有这个问题 - 但话又说回来,我也没有在库中定义我的部分类。
I've had similar problems with an application that I started with the beta release. The only thing I've found that works is to create a code-behind file for the control and define the inheritance in the code-behind. Then in the control, have it inherit from the class defined in your code-behind. In newer projects, built from scratch using 1.0, I don't have this problem -- but then again I don't have my partial classes defined in a library, either.
成功! 不知何故,我的 .ascx 文件在文件属性中被标记为“内容”而不是“编译”。 一旦解决了这个问题,我现在就可以引用必要的程序集并毫无问题地加载/运行插件。 感谢您对代码隐藏文件的提示!
(更新)我昨晚发现的另一个提示是我的插件也必须位于应用程序的 /bin 目录以及我的 /widgets 目录中,否则我会收到“无法加载类型...”异常。 我还没有弄清楚如何连接 ASP.NET MVC,所以我只需将它放在 1 个位置,但是当我这样做时,我会在此处发布。
Success! Somehow, my .ascx file had been marked "content" in the file properties instead of "compile". Once I fixed that, I can now reference the necessary assemblies and load/run the plugin without problems. Thanks for the tip on the codebehind file!
(update) The other tip I figured out last night is that my plugin also has to be in the app's /bin directory as well as my /widgets directory, otherwise I get a "Could not load type..." exception. I haven't figured out yet how to wire up ASP.NET MVC so I only have to have it in 1 place, but when I do, I'll post back here.