简体   繁体   English

ASP.NET Web表单作为ASP.NET MVC

[英]ASP.NET web forms as ASP.NET MVC

I am sorry for possible misleading about the title, but I have no idea for a proper title. 我很抱歉可能误导了这个头衔,但我不知道一个合适的头衔。

Feel free to edit. 随意编辑。

Anyway, I am using ASP.NET Web Forms, and maybe this isn't how web forms is intended to be used, but I like to construct and populate HTML elements manually. 无论如何,我使用的是ASP.NET Web Forms,也许这不是Web表单的使用方式,但我喜欢手动构建和填充HTML元素。 It gives me more control. 它给了我更多的控制权。

I don't use DataBinding and that kind of stuff. 我不使用DataBinding和那种东西。 I use SqlConnection, SqlCommand and SqlDataReader, set SQL string etc. and read the data from the DataReader. 我使用SqlConnection,SqlCommand和SqlDataReader,设置SQL字符串等,并从DataReader读取数据。 Old school if you like. 老学校,如果你喜欢。 :) :)

I do create WebControls so that I don't have to copy-paste every time I need some control, but mostly, I need WebControls to render as HTML so I can append that HTML into some other function that renders the final output with the control inside. 我确实创建WebControls,这样我每次需要一些控件时都不需要复制粘贴,但大多数情况下,我需要WebControls渲染为HTML,这样我就可以将HTML附加到其他函数中,使用控件渲染最终输出内。

I know I can render a control with control.RenderControl(writer), but this can only be done in (pre)Render or RenderContents overrides. 我知道我可以用control.RenderControl(writer)渲染一个控件,但这只能在(pre)Render或RenderContents覆盖中完成。

For example. 例如。

I have a dal.cs file where is stored all static functions and voids that communicate with the database. 我有一个dal.cs文件,其中存储了与数据库通信的所有静态函数和空洞。 Functions mostly return string so that it can be appended into some other function to render the final result. 函数主要返回字符串,以便可以将其附加到其他函数中以呈现最终结果。

The reason I am doing like this is that I want to separate the coding from the HTML as much as I can so that I don't do <% while (dataReader.Read()) %> in HTML and display the data. 我这样做的原因是我想尽可能多地将编码与HTML分开,这样我就不会在HTML中执行<%while(dataReader.Read())%>并显示数据。 I moved this into a CodeBehind. 我把它移到了CodeBehind。 I also use this functions to render in the HttpHandler for AJAX response. 我还使用此函数在HttpHandler中呈现AJAX响应。

That works perfectly, but when I want to add a control (ASP.NET Server control (.cs extension, not .ascx)) I don't know how to do that, so I see my self writing the same control as function that returns string or another function inside that control that returns string and replaces a job that would RenderContents do, so that I can call that function when I need control to be appended into a another string. 这很好用,但是当我想添加一个控件(ASP.NET服务器控件(.cs扩展名,而不是.ascx))时,我不知道该怎么做,所以我看到自己编写了与函数相同的控件返回该控件中的字符串或其他函数,它返回字符串并替换RenderContents所做的作业,这样当我需要将控件附加到另一个字符串时,我可以调用该函数。 I know this may not be a very good practice. 我知道这可能不是一个很好的做法。

As I see all the tutorials/videos about the ASP.NET MVC, I think it suite my needs as with the MVC you have to construct everything (or most of it) by your self, which I am already doing right now with web forms. 当我看到关于ASP.NET MVC的所有教程/视频时,我认为它满足了我的需求,就像MVC一样,你必须自己构建一切(或大部分),我现在已经在使用Web表单。

After this long intro, I want to ask how can I build my controls so I can use them as I mentioned (return string) or I have to forget about server controls and build the controls as functions and used them that way? 在这个长篇介绍之后,我想问一下如何构建我的控件以便我可以像我提到的那样使用它们(返回字符串)或者我必须忘记服务器控件并将控件构建为函数并以这种方式使用它们? Is that even possible with ASP.NET Server Controls (.cs extension) or am I right when I said that I am not using it right. 甚至可以使用ASP.NET服务器控件(.cs扩展名),或者当我说我没有正确使用它时我是对的。

To be clear, I am talking about how to properly use a web forms, but to avoid data binders because I want to construct everything by my self (render HTML in Code Behind). 为了清楚起见,我正在讨论如何正确使用Web表单,但是为了避免使用数据绑定器,因为我想通过自己构建所有内容(在Code Behind中呈现HTML)。
Someone might think that I am appending strings like "some " + "string", which I am not. 有人可能会认为我附加的字符串如“some”+“string”,我不是。 I am using StringBuilder for that so there's no slowness. 我正在使用StringBuilder,因此没有缓慢。

Every opinion is welcome. 欢迎提出任何意见。

You need to stop thinking in terms of "controls" and webforms. 你需要停止思考“控件”和webforms。 MVC is a completely different way of constructing applications. MVC是一种完全不同的构建应用程序的方式。

I also hate the automatic renderers in WebForms, they produce horrible html that never makes any sense. 我也讨厌WebForms中的自动渲染器,它们产生了可怕的html,从来没有任何意义。 However, you dont want to be writing your html in your codebehind and passing it around as strings, thats just nasty. 但是,你不想在你的代码隐藏中编写你的html并将其作为字符串传递,这只是令人讨厌的。 Your presentation code is mixed in with your logic, AND youre writing HTML in c# strings!!! 您的演示代码与您的逻辑混合在一起,并且您在c#字符串中编写HTML!

So, MVC... Instead of "widgets" that do interacty things with codebehinds and postbacks, yours view ONLY display data and contain forms to allow you to post to the controllers. 因此,MVC ...您可以查看显示数据并包含允许您发布到控制器的表单,而不是使用代码隐藏和回发进行交互操作的“窗口小部件”。

Because of this, you can strongly type your views to a Type, and then access the data you pass to it from a controller via the Model property. 因此,您可以强烈地键入视图到Type,然后通过Model属性访问从控制器传递给它的数据。 The equivalent to UserControls are Partial Views (ViewUserControl) which can be used to modularise your rendering code for types. 与UserControls等效的是部分视图(ViewUserControl),可用于模块化类型的渲染代码。 For example, you might make an Address partial to which you pass your Person's Address property every time you need it rendered. 例如,您可能会在每次需要渲染时传递Person的Address属性。 This way you aren't repeating html all over the place. 这样你就不会在整个地方重复html。

PS A single file for all your DAL? PS所有DAL的单个文件?

I hope I never ever have to work on an app you wrote in this manner. 我希望我永远 不会以这种方式处理您编写的应用程序。 If your application is string-heavy then something is wrong. 如果您的应用程序是字符串繁重,那么有些问题。

Agree with @sliderhouserules, the way you are using MVC framework is awful. 同意@sliderhouserules,你使用MVC框架的方式很糟糕。 You must forgot all your "old school" techniques. 你必须忘记所有的“旧学校”技巧。 You should never use SqlCommands, SqlReaders, etc. in the code of the pages. 你永远不应该在页面代码中使用SqlCommands,SqlReaders等。 You should pass to the view only a model (eg View(bar)) and it will be better if you avoid usage of 你应该只传递一个模型(例如View(bar)),如果你避免使用它会更好

ViewData["some magic string"] = bar

Every time when you will use "old school" technique 2 mans and 2 cats will be killed :). 每次当你使用“老派”技术时,2个男人和2只猫将被杀死:)。
Also it's better to use some ORM (Object-Relational Mapper) like Linq2sql, NHibernate, SubSonic, etc. 另外最好使用一些ORM(对象关系映射器),如Linq2sql,NHibernate,SubSonic等。
If you need in samples of good application design please look at SharpArchitecture . 如果您需要良好应用设计的样品,请查看SharpArchitecture It has a very good architecture and implementation and may help a lot. 它有一个非常好的架构和实现,可能会有很多帮助。 It has one sample (with Northwind db) and one more sample will be added soon. 它有一个样本(使用Northwind数据库),很快就会添加一个样本。
Also look at CodeCampServer . 另请参阅CodeCampServer It has very good architecture too. 它也有非常好的架构。
It's better to look at the code of these projects instead of looking videos because existing videos can't demonstrate good sample of architecture, just a simple usage of functionality. 最好是查看这些项目的代码而不是查看视频,因为现有的视频无法展示出良好的架构样本,只是简单的功能使用。
About server controls, you may use them if they can be used without 'runat="server"', like PlaceHolder. 关于服务器控件,如果没有'runat =“server”'就可以使用它们,就像PlaceHolder一样。 And you may create them too, but you shouldn't load any data in them directly. 您也可以创建它们,但不应直接在其中加载任何数据。 If you don't want to copy-paste html you should review your code and you should refactor it. 如果你不想复制粘贴html,你应该检查你的代码,你应该重构它。 Every duplicated code should be moved to MasterPages of UserControls (ascx ones). 应将每个重复的代码移动到UserControls的MasterPages(ascx)。
And once more, please spend some time to look at these samples. 再一次,请花点时间看看这些样品。 You'll save your nerves and time in the future when you'll need to update the app or fix something. 当您需要更新应用程序或修复某些内容时,您将在以后节省您的紧张和时间。 At the first look they can be hard to understand but this is only at the first look. 初看起来他们很难理解,但这只是初看起来。 Hope this helps. 希望这可以帮助。

@lopkiju, I think that the MVC pattern would serve you much better than your current WebForms solution if you want that much control over the output HTML. @lopkiju,我认为如果你想要对输出HTML进行大量控制,MVC模式将比你当前的WebForms解决方案更好。
You can use Web Forms this way as you already do, but it is not designed to be used this way, so it will be a pain. 您可以像现在这样使用Web窗体,但它不是以这种方式使用,因此会很痛苦。

More detail 更多详情

In my opinion, read some articles about the Separation of Concerns (also known as SoC) principle. 在我看来,阅读一些关于分离关注 (也称为SoC)原则的文章。 If you apply it correctly, it can save you many-many headaches when you'll debug your app, and also for those people who you work with (eg. those who may have to read or modify your source code). 如果您正确应用它,它可以在您调试应用程序时为您节省许多麻烦,也为那些与您合作的人(例如那些可能必须阅读或修改您的源代码的人)保存。

My other tip for you is this: 我的另一个提示是:
You are right that you shouldn't do things like <% while (dataReader.Read()) %> in your View code. 你是不对的,你不应该在你的View代码中做<% while (dataReader.Read()) %> But perhaps there are better ways to make it more elegant than your current way. 但也许有更好的方法让它比你现在的方式更优雅。

I think you should consider using some sort of ORM for these purposes. 我认为您应该考虑使用某种ORM来实现这些目的。 (LINQ to SQL, or even NHibernate.) If you get it, it will be much simpler. (LINQ to SQL,甚至NHibernate。)如果你得到它,它会更简单。 So much that you may not want to use DataReader s directly again. 您可能不希望再次直接使用DataReader :-) :-)

What I recommend for you 我推荐给你的是什么

Simply, read the NerdDinner tutorial , and build the samle app by yourself step-by-step. 简单地说,阅读NerdDinner教程 ,并逐步构建samle应用程序。
After that, try to build a similar app that serves a different purpose by yourself while applying the same rules and design that you applied to the tutorial. 之后,尝试构建一个类似的应用程序,在应用您应用于本教程的相同规则和设计时,自己提供不同的用途。

I'm pretty sure you have the expertise to do it, and after actually doing something with it, you can pretty much get the feel of it. 我很确定你有这方面的专业知识,并且在实际使用它之后,你几乎可以感受到它。

The tutorial also explains and includes the principles I mentioned above, which will be very much use to you. 本教程解释并包含了我上面提到的原则,这些原则对您非常有用。

If you want the ASP.NET MVC path, you can set up controls as ASCX and they will simply be tags filled by the controller. 如果您需要ASP.NET MVC路径,可以将控件设置为ASCX,它们只是由控制器填充的标签。 It may not be old school enough for you, however. 但是,对你来说,这可能不够老。

You can create your full UI in code behind, if you so desire. 如果您愿意,可以在后面的代码中创建完整的UI。 This can include creating the HTML in routines. 这可以包括在例程中创建HTML。

But, I would recommend, if you are sticking with ASP.NET, reconsidering the bind model over the DataReader.Read() and output + loop. 但是,我建议,如果您坚持使用ASP.NET,请重新考虑绑定模型,而不是DataReader.Read()和输出+循环。 It is not only old style, it is highly inefficient and hard to maintain. 它不仅旧式,效率低,难以维护。

ASP.NET MVC does create a much looser ASPX, as it is just a view. ASP.NET MVC确实创建了一个更宽松的ASPX,因为它只是一个视图。 And there is a much greater separation of code and UI. 并且代码和UI之间存在更大的分离。 But it will not fit with the old school model either. 但它也不适合旧的学校模式。

Have you considered using micro-templates ? 你考虑过使用微模板吗? Dave Ward has a good example of of a client side data repeater that is uses micro-templates for layout after making a call to a page method. Dave Ward有一个很好的客户端数据中继器示例,它在调用页面方法后使用微模板进行布局。 This sounds like this is more in the spirit of what you are trying to accomplish and can still be integrated nicely with WebForms. 听起来这更像是你想要完成的精神,并且仍然可以很好地与WebForms集成。

The side effect will be that you will not rely on passing around HTML and can isolate your presentation from your logic. 副作用是您不会依赖于传递HTML并且可以将您的演示文稿与逻辑隔离开来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM