My web application is an Asp.Net MVC pattern. None of the .aspx pages have code behind files. I was told that since the application follows MVC pattern, the .aspx pages don’t have a codebehind file. All the C# coding is done inline.
My question is that, is it that we shouldn’t have code behind files for .aspx pages if we follow MVC pattern. I don’t see any difference between these .aspx pages and traditional ASP pages where we do inline coding. In order to take advantage of Asp.Net and maitaining the code / html shouldn’t we have a code behind file for every .aspx page?
It would be great if someone could clearly explain this.
Thanks in advance.
Asp.net MVC doesn’t use codebehinds that’s true. But. You shouldn’t think of your pages as traditional ASP pages, because they’re not. Majority of processing (as in codebehind) is done inside controller actions. ASPX pages barely display results of that processing (when action result is
ViewResultorPartialViewResult).So we could say that controller actions "are" codebehinds for many ASPX files.
That’s of course a very broad simplification of this, but you could understand it this way.
Additional explanation
You may argue that writing C# code in an ASPX file is very similar to writing ASP code, but let me make you think of it for a moment. In Asp.net WebForms whenever you added
what you basically did, was a
foreachstatement in a different form with some additional capabilities. In Asp.net MVC what you write is an actualforeachstatement instead with any additional functionality that you want. Majority of time you’ll just render items without additional functionality soforeachwill be:Why can a controller action be a codebehind for many ASPXs?
Because we can have this kind of code
Not to even mention that what this code does is so much simpler than some complex page lifecycle processing that would need to handle both states which would lead to much more complex ASPX file having all the HTML.
In Asp.net MVC code is very simple/basic because it only does what it needs to do and views are much simpler just as well, because each may only serve one single state/situation/purpose. No multifaceting as in Asp.net WebForms.