Answer the question
In order to leave comments, you need to log in
ASP.NET MVC, best practices?
Good afternoon. I'm learning ASP.NET MVC 5. I don't have asp.net's in a professional environment, so I have to turn here. In the course of studying the materiel, I made some conclusions for myself regarding the correct use of this technology. I need to make sure the conclusions are correct. Below is a list of statements, for each item of which please put +/- or corrective comments in your answers. Of course, all statements are for some average case, average project, average complexity with "average" requirements.
1) Routes should be as short as possible
2) Is setting routes with RouteConfig preferable to routing with attributes (or when should attributes still be used)?
3) Forms and HTML elements are generated using HtmlHelper if possible
4) Action methods should be as short as possible. The logic of validation, authentication, error handling should be in third-party classes and connected using attributes
5) Data validation should occur both on the client side (JQuery) and on the server side
6) The view should contain only auxiliary c # code for building markup. And this code
should
be
minimal the standard signature binder
will create a new Entity instance each timevoid Action(int entityId);
void Action(Entity currentEntity);
void Action(Entity currentEntity);
8) ViewBag is a crutch that should only be used for simple cases (note: passing a title to a page)
9) Ajax forms are preferable to naked Ajax. If both approaches suggest themselves in one view, then the view should be refactored
10) Everything that can be done in the markup with Razor should be done with Razor
Answer the question
In order to leave comments, you need to log in
I'm certainly not a guru, but I'll try to voice my opinion.
1) Generally speaking, yes. But what do you think is a short route? short words or few slashes? There is still a readability swing nearby.
2) I think yes, and the reason is quite banal - I don't want to climb the whole project to find out why this route fell into this controller. But there is an exception - regions (Areas). They have their own RouteConfig and it is possible that it will work differently from the default one.
3) It's not a problem at all. No one will forbid you to write Javascript logic, which then along the way will take more data through the API and draw the view itself. Do whatever you want, but it should be readable, maintainable, and at least a little relevant to the task at hand (don't use Angular just to use Angular)
4) Here you mixed everything together. The shortest possible? In principle, yes, but I would like to read the title to guess exactly what it does, and not see a surprise later. I also agree with the rest.
5) Suppose it is needed. First, remove the word JQuery - this library has little to do with validation. Secondly, on the server, as a rule, there are enough checks of the ModelState.isValid and Model.isValid properties. Of course, if your model is marked with all the attributes that limit the correct values. There is really one catch here - suppose your validation logic is changing (the fields have been updated or some other gestures have been made). In practice, you need to update the same logic in two places, which is not very correct (potential code duplication, potential forgetfulness errors).
6) Maybe yes, maybe not. I repeat - the client side can be molded in different ways. Javascript is also possible. In ASP.NET, this is not forbidden. This is generally not allowed anywhere.
UPD: It's not worth programming in C# inside the View. Drag additional data too, although occasionally such a need arises (drag some dictionary). Basically, the code should contain helper functions - for example, to form some kind of phrase or check the state of an object by several fields, instead of writing a bunch of if in the markup.
END UPD.
7) To be honest, I don’t understand what you mean.
8) This is not a crutch, it is a transport system for short messages and simple types. It can also be used as a transport for a model, but it is not safe from the point of view of type casting.
9) What do you mean by "naked Ajax"? XmlHttpRequest? Yes, in my opinion it is better not to use it. I can't comment on the second part of the question without answering the first. Views are different, some pull up additional data along the way. We need some specifics here.
10) I do not agree and have already voiced it earlier. Razor is one of the available tools. You can use it - it's good, or you can not use it.
1 and 2 have already been answered.
3. Everything is better on the front side of the SPA. Razor is a powerful templating engine, but....
4. Yes, this is normal practice.
5. Yes.
6. No one will probably kick with their feet, but damn it, no one has canceled the principle of division of responsibility.
7. Use ViewModel.
8. No.
nine.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question