If Asp.net Pages Have Code Files in MVC

My web application is an Asp.Net MVC template. None of the .aspx pages have code for files. I was told that since the application follows the MVC pattern, the .aspx pages do not have a codebehind file. All C # coding is done in strict format.

My question is that we should not have code behind the files for the .aspx pages if we follow the MVC pattern. I do not see the difference between these .aspx pages and traditional ASP pages where we do inline coding. To take advantage of Asp.Net and maitaining the / html code, should I have code behind the file for each .aspx page?

It would be great if anyone could explain this.

Thanks in advance.

+4
source share
4 answers

Asp.net MVC does not use codebehinds, which is true. But. You should not think of your pages as traditional ASP pages, because it is not. Most of the processing (as in the code) is performed inside the controller actions. ASPX pages barely display the results of this processing (when the result of a ViewResult or PartialViewResult ).

Thus, we could say that the controller actions are β€œcodes” for many ASPX files.

This, of course, is a very broad simplification of this, but you could understand it that way.

Additional explanation

You may argue that writing C # code in an ASPX file is very similar to writing ASP code, but let me think about it for a moment. In Asp.net WebForms whenever you added

 <asp:Repeater ... /> 

what you basically did was a foreach in a different form with some additional features. In Asp.net MVC, what you write is the actual foreach , and not any additional functionality that you want. In most cases, you will simply create elements without additional functionality, so foreach will be:

  • faster than rich server control
  • easier since the HTML for each element will be ok. same as repeater element template
  • fewer hungry resources, because it is just a simple language construct, and not some kind of server control that needs to be created, executed and saved (as it is able to)

Why can a controller action be the code for many ASPX?

Since we can have code like this

 public ActionResult Index() { var data = /* get some data */ if (data.Count > 0) { /* first ASPX */ return View("DisplayItems", data); } else { /* second ASPX */ result View("DisplayEmpty"); } } 

Not to mention that what this code does is much simpler than complex page lifecycle processing, which would have to handle both states that would lead to a much more complex ASPX file that has all the HTML.

In Asp.net code, MVC is very simple / basic, because it only does what it needs, and views are much simpler, because everyone can serve only one state / situation / purpose. There are no multi-files like in Asp.net WebForms.

+5
source

None of the .aspx pages have code for files

How it should be. The traditional code in WebForms, in which you subscribe to various page life cycle events, no longer needs to be done in MVC. This task is performed by the controller. Of course, your .aspx pages may contain some server-side embedded code to use HTML and Url helpers such as TextBoxFor, EditorFor, ActionLink ... But that is where you should limit your views.

Take, for example, another viewing engine, such as Razor. The concept of code behind does not even exist for him. So the fact that you can still have code in the WebForms viewer is that it is a legacy of the classic ASP.NET WebForms and not used in MVC.

+2
source

There are no code delays in MVC. MVC just uses aspx in the same way as the template engine (possibly to release quickly because they are now switching to Razor as their preferred template modeling engine).

0
source

No submissions should contain code behind files (be it browsing web forms or browsing razors). The essence of mvc is that you should never have logic in your views, and they should be stupid in most cases. In most cases, you can use a loop (many use editor templates to avoid a loop) and conditional statements to render HTML conditionally. So, when you have no logic in the views, there is no need for code at all. Every thing in the Model needs to be set when in control of your views. If you think that there is a specific viewing logic

0
source

All Articles