ASP.NET MVC > ASP.NET WebForms, Why?

后端 未结 9 1005
甜味超标
甜味超标 2021-02-01 08:25

I\'ve now completed my first web application using ASP.NET MVC and overall, I still am not grasping why this is getting all the praise and glory. Maybe I\'m being stubborn. I k

9条回答
  •  面向向阳花
    2021-02-01 08:57

    Not a complete answer, but one big concern is testability of ASP.NET Forms, or the lack thereof. Testing the UI logic of what gets displayed and how. With ASP.NET Forms, you are left to just the codebehind.

    Now, MVC isn't for everyone. You may want to look into MVP (Model-View Presenter) for ASP.NET Forms as it uses very similar MVC concepts, except the Presenter is in control of changing the view's internals.

    But, testability is really a big plus for testing your code. Such as whawt happens when someone clicks the ChangePassword method/action:

    [TestClass]
    public class AccountControllerTest
    {
    
        [TestMethod]
        public void ChangePasswordPostRedirectsOnSuccess()
        {
            // Arrange
            AccountController controller = GetAccountController();
    
            // Act
            RedirectToRouteResult result = 
                (RedirectToRouteResult)controller.ChangePassword(
                    "oldPass", "newPass", "newPass");
    
            // Assert
            Assert.AreEqual("ChangePasswordSuccess"
                , result.RouteValues["action"]);
        }
    }
    

提交回复
热议问题