Where is the equivalent of WebForms front page files in ASP.NET MVC?

Today is my first day working with MVC, and I'm trying to convert my existing website to an MVC 4 site.

I read it several times, and I'm starting to understand how everything works, but I can’t understand what for new layouts (replacing MasterPages) , where is this equivalent to the file code ? On my current site, I have a main page that defines the general appearance, but also runs some code in the code to dynamically change several things (for localization and the system of generated databases).

So, now that I am using MVC and layouts, I cannot figure out where I will quote all of this, can someone point me in the right direction?

(I know that MVC does not have code behind which it uses controllers for it.)

+4
source share
3 answers

As you know, MVC is a three-layer architecture.

  • Model
  • View
  • controller

A model is data objects. You need to save or show data.

Views are the level of html or presentation that will be displayed to users.

The controller is the code underlying the file, all of your code will be in the controller. It receives data from models and applies business logic, and then goes to views to display or get updated data from a view and go to models, and then save it to the database.

File

_layout.cshtml is present on the path ~/Views/Shared/_Layout.cshtml . This is the master page in mvc. You will see that your partial views contain

 Layout = "~/Views/Shared/_Layout.cshtml"; 

this line at the top of the page. You can change the main page for any kind, and you can have several layouts.

The layout contains many partial views, such as left navigation, top navigation, and content. each of which can be configured from the controller.

Here are some links that may help you:

+3
source

Create a base controller class and make all your controllers inherit from it.

The MVC equivalent of the WebForms homepage is the base controller in which you can place the code needed for multiple controllers.

How can I execute common code for each request?

+2
source

You cannot find examples of what you are trying to do, because this is not how it is done in MVC. There is no equivalent to code delays.

You are trying to do something wrong. MVC layouts are just template files. They have no code, and they should not have any functions other than simple mapping logic.

MVC is another paradigm from WebForms. You do not use server controls such as WebForms. Therefore, the idea that you have content in a layout that does this violates the principles of MVC.

You are basically stuck in what is known as the XY issue. That you are trying to achieve certain functionality of X, and you think you need to do Y, so all you ask is Y ... when X is what you really need to ask.

Please explain what you are trying to do, and do not assume that it should be done the way you did it. For example, if you want to localize something, ask how to localize something. If you want to find dynamic content somewhere, ask how to do it, but you need to be more specific regarding these individual issues, and not just disguise them as you did.

+1
source

Source: https://habr.com/ru/post/1485315/


All Articles