asp.net mvc and code-behind files

Source: Internet
Author: User
Tags visual studio

The asp.net MVC community discusses the need for code-behind files when using Webformsviewengine, and whether this is an advantage or disadvantage or even a problem.

asp.net mvc still uses webformsviewengine by default. When you add a WebForms view to a asp.net MVC application using Visual Studio, a code-behind file and a designer file are automatically added. Many people think that code-behind files have been deprecated, and their usage is confusing because developers tend to use views like the traditional, asp.net Web forms.

Steve Smith further demonstrates the disadvantage of ASP.net MVC codebehind files because they lure developers to add logic to the view, which should be designed to remain "dumb":

Code-behind files are a temptation. Developers who are unfamiliar with asp.net mvc (asp.net mvc is less than a year old and not yet published) but have Web Forms development backgrounds (most) have a tendency to write code to hidden code files. This makes the logic in the view difficult to test, possibly even the logic contained in the code-behind file that invokes the database directly or the Web service so that it completely bypasses the separation of the model from the view.

Steve explains that in some cases the code-behind file is a bad but essential thing, such as if you want to reference the model in a strongly typed view by a strong type. Some people think that hidden code pages can also be used to store some of the view logic, while other ways may be confusing ASPX files. Last but not least, they have the support for smart tip technology in ASPX files. As for the first question, Tim Barcz demonstrates how to use a strongly typed ViewData with no code-behind, replacing the C # or VB notation by merging the CLR representations of generics:

inherits= "System.Web.Mvc.ViewPage ' 1 [[ABCCompany.MVC.Web.Models.LoginData, ABCCompany.MVC.Web]]"

The CLR notation specifies the number of generic parameters, followed by the arguments and the assembly that contains the parameter type, after the single quotation marks.

Luis Abreu did not agree with Steve's usage and responded to the posting. "It's not certain that removing hidden code files can make up for an ' unqualified ' programmer," he says, because logic can still be placed in the script block of an ASPX file. Luis also disagreed with the general view of the "dumb" view. He thought the view might have mixed "show related code" and that the code should be placed in a hidden code file rather than in a controller or directly in an ASPX file, which would break the clear separation between the markup and the code.

View English Original: Http://www.infoq.com/news/2008/09/aspnet-mvc-codebehind

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.