开发者

Best Practices for mapping MVC Controllers to Views?

开发者 https://www.devze.com 2023-01-16 09:26 出处:网络
What 开发者_运维百科are the best practices when it comes to mapping controllers to views?For example, say for the urls mysite.com/login and mysite.com/register...I can have my LoginController and a Re

What 开发者_运维百科are the best practices when it comes to mapping controllers to views? For example, say for the urls mysite.com/login and mysite.com/register...I can have my LoginController and a RegisterController map to Login/Index.aspx and Register/Index.aspx views. Alternatively, my HomeController could handle both of these and map them to Home/Login.aspx and Home/Register.aspx.

Is one "better" than the other?


Login and Register seem like related actions that could be put in the same controller. An Account controller perhaps?

As far as better, I believe it would be easier to maintain one controller that handles similar or related actions rather than having multiple controllers. I'm not sure which is more accepted as being the "right" way to do it. Maybe just personal preference?


In this particular case I would suggest two different controllers and routes as Login and Registering are separate concerns.

Your url also benefits from being more readable. IE - mysite.com/login/ and mysite.com/register/


I too went through this dilemma last week. I ended up placing PUBLIC views like Login, Register, and Cart under a Home controller, and PRIVATE views like My Profile, Wish List, Order History, Address Book, etc. under an Account controller.

Why mix views that are for authenticated users only in the same controller that handles public pages that everyone can see?

That's my logic.


its a matter of choice. Some people are against having lots of code in the controller. they ask you to keep it as minimalistic as possible. adding a new controller does not affect your application in any way. if you have multiple actions or have a new controller will not affect the performance. Here is what i follow: if you have different actions in place for each of your page( say in your case the login and register page) then put them in a separate controller where they are grouped together. say for login you have the following actions verifyInput, forgotPassword, login, incorrectLogin etc. and for register verifyInput, regenerateCaptcha, createAccount, sendMail etc.

In this case you should definitely have two controllers

0

精彩评论

暂无评论...
验证码 换一张
取 消