【发布时间】:2019-11-06 19:28:59
【问题描述】:
在 ASP.NET MVC 中,我有 2 个“登录”操作,它们执行几乎相同的操作,但它们使用不同的模型并返回不同的视图。如何确保这些操作遵循 DRY 规则?
我尝试制作一个单独的函数来接收我需要的参数,但由于模型不同,我无法做到这一点。
这是两个动作:
Public Function Login(model As AccountViewModels.InternalLoginViewModel, location As String) As ActionResult
...
If ... Then
...
Return View(model)
End If
If Not ModelState.IsValid Then
Return View(model)
End If
...
If authenticationResult.Success Then
...
ElseIf ... Then
Dim appUser As ApplicationUser = authService.GetApplicationUser(model.EmailUsername)
...
ElseIf .. Then
Return RedirectToAction("OldAppnuserRegister", MVC_CONTROLLER_ACCOUNT, authenticationResult.OldUser.GetRouteValues(location))
ElseIf ... Then
Return RedirectToAction("Index", MVC_CONTROLLER_MANAGE, New With {.userKey = authService.GetApplicationUser(model.EmailUsername).Key.ToString})
End If
...
Return View(model)
End Function
---------------
Public Function RepLogin(model As RepSessionViewModels.InternalLoginViewModel, location As String) As ActionResult
...
If ... Then
...
Return View("~/Views/RepSession/SelectProvider.vbhtml", model)
End If
If Not ModelState.IsValid Then
Return View("~/Views/RepSession/SelectProvider.vbhtml", model)
End If
...
If authenticationResult.Success Then
...
ElseIf ... Then
Dim appUser As ApplicationUser = authService.GetApplicationUser(model.Email)
...
ElseIf ... Then
Return RedirectToAction("OldRepRegister", MVC_CONTROLLER_ACCOUNT, authenticationResult.OldUser.GetRouteValues(location))
ElseIf ... Then
Return RedirectToAction("Index", MVC_CONTROLLER_MANAGE, New With {.userKey = authService.GetApplicationUser(model.Email).Key.ToString})
End If
...
Return View("~/Views/RepSession/SelectProvider.vbhtml", model)
End Function
我将重复的代码替换为“...”以使其更具可读性。感谢所有帮助。谢谢。
【问题讨论】:
-
this question 可能有帮助
标签: asp.net-mvc vb.net dry