How many controllers mvc




















Remember also that as much as possible you shouldn't have core business logic in your controllers. They really implement the front-end behavior while the real system rules should be in your domain model and service layer. As long as you keep things roughly within the right layer and reasonably decoupled, you can't go too far wrong with how you place the individual operations within your controllers. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Better to have huge Controllers, or many controllers, in MVC? Ask Question. Asked 12 years, 3 months ago. Active 12 years, 3 months ago.

Viewed 25k times. And finally, does it even matter? For example, let's consider Create and Delete Beep beep. Beep beep Beep beep Add a comment. Active Oldest Votes. Sam Wessel Sam Wessel 8, 8 8 gold badges 39 39 silver badges 44 44 bronze badges.

This can work quite well, it is about the only reason why I would bother with a partial class. Good thought An interesting idea, but I think it abuses the partial class feature a little bit. Partial classes were designed primarily for code generation scenarios so that developers could add to a generated class and have those changes be preserved during a re-gen.

Unless I misunderstand, I think it's better to create separate controllers with a shared base class than to use partials. Since posting my answer I actually completely agree with you Seth. Partial classes have bitten me in the past and I generally avoid them now. Oh how fickle I am. SamWessel What kind of problems did you encounter specifically? Same View Different Paths That may be a better solution than using a subtype approach that you suggested.

Community Bot 1 1 1 silver badge. Not sure if "subtypes" was the best wording on my part. I wouldn't be implementing areas grouping them into folders , just creating more of them. I'll update the question with some type of example to clarify. The reality, in practice, is that MVC controllers do more than just render views. At the very least, you need a separate get and post handler for each page. Each require different inputs.

If you're lucky, the view model you render is the same view model that gets posted back. In a post, your controller at least has to unpack the view model into a DTO to pass to your business logic layer, and you'll probably have to do some kind of transform on it, because the logical breakdown of data coming from the user isn't necessarily the same as what's sent to the business logic layer.

If that's the case then all you are really doing in your controller is returning data to the view. Not really sure if I'm a fan of separating the controllers into subtypes. Whilst you should maintain separation of concerns I think subtypes is going a little too far. Also you need to be careful in cases where heavy objects are initialized in the constructor or a controller. It's better to have a controller per logic unit, for example AccountController login, registration, logout , FileController search, upload and so on.

By saying general I mean that this is the best practice. Remember that all controllers should basically handle CRUD operations over model and use different views for different filters. In my opinion one of the major advantages of MVC as pattern is that it deliver the best way to tie models and views. About the example you added: I would create 2 controller: one for the all user login operation register, login, logout etc.

Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Ask Question. Asked 8 years, 3 months ago. Active 8 years, 1 month ago. Viewed 59k times. How many controllers does a single web app need? I realize this would be difficult to answer without an example so I'll provide one: Application: User logs in.

User can do one of three things: a Upload a file stored in a mongodb database with meta data. My question is a general one, but I gave the example to help out anyone trying to answer. Improve this question. Community Bot 1. Jeff Jeff 1, 2 2 gold badges 15 15 silver badges 19 19 bronze badges. A really nicely asked question. Add a comment. Active Oldest Votes.

Improve this answer. Taking your admin example: Would the admin controller extend the general user or would you completely redefine all the methods? For example, maybe all users can upload and search but only admins can delete. Would the admin controller class just inherit all the general user methods? This really depends a lot on actual functionality. But in general I simple write a second controller without any inheritance at all.

Following the 'thin controller' principle there should not be much code in a controller anyway. And the admin controller can be especially simple.

All important functionality goes into the model. Dan Pichelman Dan Pichelman Yusubov Yusubov 21k 6 6 gold badges 43 43 silver badges 71 71 bronze badges. Great references! I'll be sure to check them out! Sure, no problem. I like Apple's way of doing it.

Korey Hinton Korey Hinton 2, 3 3 gold badges 17 17 silver badges 30 30 bronze badges. When you browse to the app and don't supply any URL segments, it defaults to the "Home" controller and the "Index" method specified in the template line highlighted above. In the preceding URL segments:. The Welcome method runs and returns the string This is the Welcome action method You haven't used the [Parameters] part of the URL yet.

Modify the code to pass some parameter information from the URL to the controller. Try different values for name and numtimes in the URL. The MVC model binding system automatically maps the named parameters from the query string to parameters in the method. See Model Binding for more information. Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services.

Privacy policy. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Part 2, add a controller to an ASP. MVC-based apps contain: M odels: Classes that represent the data of the app. The model classes use validation logic to enforce business rules for that data. Typically, model objects retrieve and store model state in a database.



0コメント

  • 1000 / 1000