将代码移出控制器并移入 .NET Core 中的辅助方法 class 的最佳方法是什么
What is the best way to move your code out of the controller and into a helper method class in .NET Core
如何将重复的代码部分从控制器中移出到辅助方法中 class 而不必在 .NET Core 中重复代码?如果我需要提供更多详细信息,请告诉我。
我需要将任何重复的代码部分移出此控制器,以便我可以在需要它的所有其他控制器中调用此方法
用户控制器:
using myApp.Data;
using myApp.Models;
using myApp.Models.ViewModels;
using myApp.Utilities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace myApp.Controllers
{
[Authorize]
public class UserController : Controller
{
private readonly ApplicationDbContext db;
private readonly UserManager<ApplicationUser> userManager;
public UserController( ApplicationDbContext db,
UserManager<ApplicationUser> userManager)
{
this.db = db;
this.userManager = userManager;
}
[HttpGet]
public async Task<IActionResult> UpdateUserDetails(UpdateUserViewModel model)
{
if (ModelState.IsValid)
{
var user = await userManager.FindByIdAsync(model.Id);
if (user == null)
{
//Calling Repeated Code in this controller
return UserNotFound();
}
else
{
user.FirstName = model.FirstName;
user.LastName = model.LastName;
user.UserName = model.UserName;
user.PhoneNumber = model.PhoneNumber;
}
var result = await userManager.UpdateAsync(user);
if (result.Succeeded)
{
//Calling Repeated Code in this controller
return UpdateSuccess();
}
AddErrors(result);
}
return View(model);
}
//REPEATED CODE SECTION BEGINS (Extracted out of UpdateUserDetails Controller)
public IActionResult UserNotFound()
{
TempData[HelperStatic.ErrorMessage] = HelperStatic.userNotFoundMsg;
return View(HelperStatic.notFoundView);
}
public IActionResult UpdateSuccess()
{
TempData[HelperStatic.SuccessMessage] = HelperStatic.recordUpdatedMsg;
return RedirectToAction(nameof(Index));
}
//REPEATED CODE SECTION ENDS
}
}
静态助手 class 已经存在于只有静态常量的项目中。
上面控制器中使用的静态助手class:
namespace myApp.Utilities
{
public static class HelperStatic
{
// Messages
public const string SuccessMessage = "Success";
public const string ErrorMessage = "Error";
public const string userNotFoundMsg = "User not found";
public const string recordUpdatedMsg = "Record updated";
// Views
public const string notFoundView = "NotFound";
}
}
我需要一个不同的 HelperMethod
class 具有可重复使用的操作方法。我该如何实现?
代替助手 class,创建一个控制器基础 class 并将所有实用程序方法移入其中。
public class BaseController : Controller
{
public IActionResult UserNotFound()
{
TempData[HelperStatic.ErrorMessage] = HelperStatic.userNotFoundMsg;
return View(HelperStatic.notFoundView);
}
public IActionResult UpdateSuccess()
{
TempData[HelperStatic.SuccessMessage] = HelperStatic.recordUpdatedMsg;
return RedirectToAction(nameof(Index));
}
}
你可以这样使用它。
public class HomeController : BaseController
{
public IActionResult Index()
{
UserNotFound();
return View();
}
}
如何将重复的代码部分从控制器中移出到辅助方法中 class 而不必在 .NET Core 中重复代码?如果我需要提供更多详细信息,请告诉我。
我需要将任何重复的代码部分移出此控制器,以便我可以在需要它的所有其他控制器中调用此方法
用户控制器:
using myApp.Data;
using myApp.Models;
using myApp.Models.ViewModels;
using myApp.Utilities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace myApp.Controllers
{
[Authorize]
public class UserController : Controller
{
private readonly ApplicationDbContext db;
private readonly UserManager<ApplicationUser> userManager;
public UserController( ApplicationDbContext db,
UserManager<ApplicationUser> userManager)
{
this.db = db;
this.userManager = userManager;
}
[HttpGet]
public async Task<IActionResult> UpdateUserDetails(UpdateUserViewModel model)
{
if (ModelState.IsValid)
{
var user = await userManager.FindByIdAsync(model.Id);
if (user == null)
{
//Calling Repeated Code in this controller
return UserNotFound();
}
else
{
user.FirstName = model.FirstName;
user.LastName = model.LastName;
user.UserName = model.UserName;
user.PhoneNumber = model.PhoneNumber;
}
var result = await userManager.UpdateAsync(user);
if (result.Succeeded)
{
//Calling Repeated Code in this controller
return UpdateSuccess();
}
AddErrors(result);
}
return View(model);
}
//REPEATED CODE SECTION BEGINS (Extracted out of UpdateUserDetails Controller)
public IActionResult UserNotFound()
{
TempData[HelperStatic.ErrorMessage] = HelperStatic.userNotFoundMsg;
return View(HelperStatic.notFoundView);
}
public IActionResult UpdateSuccess()
{
TempData[HelperStatic.SuccessMessage] = HelperStatic.recordUpdatedMsg;
return RedirectToAction(nameof(Index));
}
//REPEATED CODE SECTION ENDS
}
}
静态助手 class 已经存在于只有静态常量的项目中。
上面控制器中使用的静态助手class:
namespace myApp.Utilities
{
public static class HelperStatic
{
// Messages
public const string SuccessMessage = "Success";
public const string ErrorMessage = "Error";
public const string userNotFoundMsg = "User not found";
public const string recordUpdatedMsg = "Record updated";
// Views
public const string notFoundView = "NotFound";
}
}
我需要一个不同的 HelperMethod
class 具有可重复使用的操作方法。我该如何实现?
代替助手 class,创建一个控制器基础 class 并将所有实用程序方法移入其中。
public class BaseController : Controller
{
public IActionResult UserNotFound()
{
TempData[HelperStatic.ErrorMessage] = HelperStatic.userNotFoundMsg;
return View(HelperStatic.notFoundView);
}
public IActionResult UpdateSuccess()
{
TempData[HelperStatic.SuccessMessage] = HelperStatic.recordUpdatedMsg;
return RedirectToAction(nameof(Index));
}
}
你可以这样使用它。
public class HomeController : BaseController
{
public IActionResult Index()
{
UserNotFound();
return View();
}
}