在ASP中实现用户控件的最佳方式是什么?净MVC吗?

时间:2021-09-27 16:02:38

Like many others on this site I am considering a move to ASP.NET MVC for future projects. Currently my sites are running the traditional ASP.NET 2.0 Web Forms, and it works OK for us, so my other option is just to stick with what I know and make the move to ASP.NET 3.5 with the integrated AJAX stuff.

像这个网站上的许多其他人一样,我正在考虑迁移到ASP。NET MVC用于未来的项目。目前,我的网站正在运行传统的ASP。NET 2。0 Web Forms,它对我们来说是没问题的,所以我的另一个选择就是坚持我所知道的,然后转向ASP。NET 3.5集成了AJAX内容。

I'm wondering about how user controls work in ASP.NET MVC. We have tons of .ASCX controls, and a few composite controls. When I work with web designers it is very easy to get them to use ASCX controls effectively, even without any programming knowledge, so that's a definite plus. But then of course the downsides are the page life cycle, which can be maddening, and the fact that ASCX controls are hard to share between different projects. Composite controls are share-able, but basically a black box to a designer.

我想知道用户控件在ASP中是如何工作的。净MVC。我们有大量的。ascx控件和一些复合控件。当我与web设计人员一起工作时,很容易让他们有效地使用ASCX控件,即使没有任何编程知识,所以这是一个明显的优点。但是,当然,缺点是页面生命周期,这可能会让人抓狂,而且ASCX控件很难在不同的项目之间共享。复合控件是可共享的,但基本上是一个设计器的黑盒。

What's the model in ASP.NET MVC? Is there a way to create controls that solves the problems we've dealt with using ASCX and composite controls? Allowing easy access for web designers without having to worry about code being broken is an important consideration.

ASP的模型是什么?净MVC吗?有没有一种方法可以创建控件来解决我们使用ASCX和复合控件处理的问题?允许web设计人员轻松访问,而不必担心代码被破坏,这是一个重要的考虑。

4 个解决方案

#1


8  

To implement a user control you do the following call:

要实现用户控件,请执行以下调用:

<% Html.RenderPartial("~/Views/Shared/MyControl.ascx", {data model object}) %>

You may also see the older syntax which as of PR5 is not valid anymore

您可能还会看到更旧的语法,从PR5开始就不再有效

<%= Html.RenderUserControl("~/Views/Shared/MyControl.ascx", {data model object}) %>

You will always have to worry about code breaking when moving from Web Forms to MVC, however the ASP.NET MVC team has done a great job to minimize the problems.

当您从Web表单迁移到MVC时,您总是要担心代码会被破坏,但是ASP。NET MVC团队在最小化问题方面做得很好。

#2


1  

As Nick suggested, you will indeed be able to render your user controls, but obviously the page-cycle, pagestate and postback from traditional ASP Webforms won't work anymore, thus making your controls most likely useless.

正如Nick所建议的,您确实能够呈现用户控件,但是显然来自传统ASP web表单的页面周期、pagestate和回发将不再工作,因此您的控件很可能是无用的。

I think you'll have to rewrite most of your complex controls to port your website to MVC, while simple controls which, for instance, provide only formatting and have no postback status, should simply work. The code provided by Nick will simply work in this case.

我认为您将不得不重写大部分复杂的控件来将您的网站移植到MVC,而简单的控件(例如,仅提供格式且没有回发状态)应该可以简单地工作。Nick提供的代码将在这种情况下正常工作。

And about sharing between more projects: I think controls will be more like "reusable HTML-rendering components" that can be shared across a website, rather than "reusable code components" with logic (like WebForms controls). Your web logic will/should be in the pages controllers and not in the HTML controls. Therefore sharing controls across more projects won't be so useful as in the WebForms case.

关于更多项目之间的共享:我认为控件将更像“可重用的html呈现组件”,可以在网站上共享,而不是使用逻辑的“可重用代码组件”(如WebForms控件)。您的web逻辑将/应该在页面控制器中,而不是在HTML控件中。因此,在更多项目之间共享控件并不像在WebForms中那样有用。

#3


1  

MVC has different page life cycle compare to your user control.

与用户控件相比,MVC具有不同的页面生命周期。

You may consider this to re-write.

你可以考虑重写。

The aspx is the view. You still need a re-write, the syntax is different. JavaScript will work. But I hardly find the WebControls will work. Because MVC does not have viewstate and postback anymore.

aspx是视图。您仍然需要重写,语法不同。JavaScript。但我几乎找不到WebControls是否能用。因为MVC不再有viewstate和回发。

For the code behind (aspx.cs) you need to convert that to be a Controller class. Page_Load method will no longer works. You probable leave it to Index() method.

对于后面的代码(aspx.cs),需要将其转换为Controller类。Page_Load方法将不再工作。您可能把它留给Index()方法。

Model is simply the entity classes that your code behind consume.

模型只是您的代码所使用的实体类。

Conclusion, it's a total rewrite. Cheers. Happy coding.

结论是,这完全是重写。欢呼。快乐的编码。

#4


1  

Yeah, you can do RenderPartial. That's a good start. But eventually these guys will need logic and other controller type stuff. Be on the lookout for a subcontroller implementation from the framework team. There should also be something in MvcContrib soon. Or roll your own.

你可以做渲染偏置。这是一个好的开始。但最终这些家伙需要逻辑和其他控制器类型的东西。注意框架团队的子控制器实现。mvc悔药里也应该有一些东西。或者自己滚。

Edit: I just posted about this here: http://mhinze.com/subcontrollers-in-aspnet-mvc/

编辑:我刚刚在这里发布了这个:http://mhinze.com/subcontrollers-in-aspnet-mvc/

#1


8  

To implement a user control you do the following call:

要实现用户控件,请执行以下调用:

<% Html.RenderPartial("~/Views/Shared/MyControl.ascx", {data model object}) %>

You may also see the older syntax which as of PR5 is not valid anymore

您可能还会看到更旧的语法,从PR5开始就不再有效

<%= Html.RenderUserControl("~/Views/Shared/MyControl.ascx", {data model object}) %>

You will always have to worry about code breaking when moving from Web Forms to MVC, however the ASP.NET MVC team has done a great job to minimize the problems.

当您从Web表单迁移到MVC时,您总是要担心代码会被破坏,但是ASP。NET MVC团队在最小化问题方面做得很好。

#2


1  

As Nick suggested, you will indeed be able to render your user controls, but obviously the page-cycle, pagestate and postback from traditional ASP Webforms won't work anymore, thus making your controls most likely useless.

正如Nick所建议的,您确实能够呈现用户控件,但是显然来自传统ASP web表单的页面周期、pagestate和回发将不再工作,因此您的控件很可能是无用的。

I think you'll have to rewrite most of your complex controls to port your website to MVC, while simple controls which, for instance, provide only formatting and have no postback status, should simply work. The code provided by Nick will simply work in this case.

我认为您将不得不重写大部分复杂的控件来将您的网站移植到MVC,而简单的控件(例如,仅提供格式且没有回发状态)应该可以简单地工作。Nick提供的代码将在这种情况下正常工作。

And about sharing between more projects: I think controls will be more like "reusable HTML-rendering components" that can be shared across a website, rather than "reusable code components" with logic (like WebForms controls). Your web logic will/should be in the pages controllers and not in the HTML controls. Therefore sharing controls across more projects won't be so useful as in the WebForms case.

关于更多项目之间的共享:我认为控件将更像“可重用的html呈现组件”,可以在网站上共享,而不是使用逻辑的“可重用代码组件”(如WebForms控件)。您的web逻辑将/应该在页面控制器中,而不是在HTML控件中。因此,在更多项目之间共享控件并不像在WebForms中那样有用。

#3


1  

MVC has different page life cycle compare to your user control.

与用户控件相比,MVC具有不同的页面生命周期。

You may consider this to re-write.

你可以考虑重写。

The aspx is the view. You still need a re-write, the syntax is different. JavaScript will work. But I hardly find the WebControls will work. Because MVC does not have viewstate and postback anymore.

aspx是视图。您仍然需要重写,语法不同。JavaScript。但我几乎找不到WebControls是否能用。因为MVC不再有viewstate和回发。

For the code behind (aspx.cs) you need to convert that to be a Controller class. Page_Load method will no longer works. You probable leave it to Index() method.

对于后面的代码(aspx.cs),需要将其转换为Controller类。Page_Load方法将不再工作。您可能把它留给Index()方法。

Model is simply the entity classes that your code behind consume.

模型只是您的代码所使用的实体类。

Conclusion, it's a total rewrite. Cheers. Happy coding.

结论是,这完全是重写。欢呼。快乐的编码。

#4


1  

Yeah, you can do RenderPartial. That's a good start. But eventually these guys will need logic and other controller type stuff. Be on the lookout for a subcontroller implementation from the framework team. There should also be something in MvcContrib soon. Or roll your own.

你可以做渲染偏置。这是一个好的开始。但最终这些家伙需要逻辑和其他控制器类型的东西。注意框架团队的子控制器实现。mvc悔药里也应该有一些东西。或者自己滚。

Edit: I just posted about this here: http://mhinze.com/subcontrollers-in-aspnet-mvc/

编辑:我刚刚在这里发布了这个:http://mhinze.com/subcontrollers-in-aspnet-mvc/