Former-commit-id: d3c853a157c7c757353eb78af0675f68e6af6e39
TangShanKaiPing
wanggang 5 years ago
parent 904537ad07
commit d7f544da5c

@ -2,10 +2,10 @@ using Microsoft.AspNetCore.Mvc;
namespace Infrastructure.Web
{
[ApiExplorerSettings(IgnoreApi = true)]
public class BaseController : Controller
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1054:URI 参数不应为字符串", Justification = "<挂起>")]
[ApiExplorerSettings(IgnoreApi = true)]
public IActionResult RedirectTo(string action = "Index", string controller = null, string rawMesage = "操作成功", object routeValues = null, string returnUrl = null)
{
if (returnUrl is null)

@ -12,6 +12,9 @@ using System.Linq;
namespace Infrastructure.Web.Mvc
{
[ApiExplorerSettings(IgnoreApi = true)]
[ApiController]
[Route("Admin/[controller]/[action]")]
public class CrudController<TEntity, TSearchModel, TDisplayModel, TEditModel> : BaseController
where TEntity : BaseEntity
where TSearchModel : PagedListModel<TDisplayModel>
@ -23,6 +26,7 @@ namespace Infrastructure.Web.Mvc
this._repo = repo;
}
[HttpGet]
public virtual IActionResult Index(TSearchModel model)
{
if (model == null)
@ -50,9 +54,10 @@ namespace Infrastructure.Web.Mvc
.ToList());
ViewData["EntityTypeExt"] = typeof(TEntity);
ViewData["ModelTypeExt"] = typeof(TDisplayModel);
return View(model);
return this.Result(model);
}
[HttpGet]
public virtual IActionResult Details(Guid id)
{
var query = this._repo.ReadOnlyTable();
@ -60,9 +65,10 @@ namespace Infrastructure.Web.Mvc
var entity = query.FirstOrDefault(o => o.Id == id);
var model = entity.To<TDisplayModel>();
this.ToDisplayModel(entity, model);
return View(model);
return Result(model);
}
[HttpGet]
public virtual IActionResult Add()
{
var model = Activator.CreateInstance<TEditModel>();
@ -97,6 +103,7 @@ namespace Infrastructure.Web.Mvc
return View(model);
}
[HttpGet]
public virtual IActionResult Edit(Guid id)
{
var query = this._repo.ReadOnlyTable();
@ -137,6 +144,7 @@ namespace Infrastructure.Web.Mvc
return View(model);
}
[HttpPost]
public virtual IActionResult Remove(List<Guid> list)
{
if (list == null)
@ -161,6 +169,7 @@ namespace Infrastructure.Web.Mvc
}
}
[HttpPost]
public IActionResult Restore(List<Guid> list)
{
if (list == null)
@ -185,6 +194,7 @@ namespace Infrastructure.Web.Mvc
}
}
[HttpPost]
public virtual IActionResult Delete(List<Guid> list)
{
if (list == null)
@ -212,44 +222,54 @@ namespace Infrastructure.Web.Mvc
}
}
[ApiExplorerSettings(IgnoreApi = true)]
public virtual IQueryable<TEntity> Include(IQueryable<TEntity> query)
{
return query;
}
[ApiExplorerSettings(IgnoreApi = true)]
public virtual IQueryable<TEntity> Query(TSearchModel model, IQueryable<TEntity> query)
{
return query;
}
[ApiExplorerSettings(IgnoreApi = true)]
public virtual void ToEditModel(TEntity entity, TEditModel model)
{
}
[ApiExplorerSettings(IgnoreApi = true)]
public virtual void ToDisplayModel(TEntity entity, TDisplayModel model)
{
}
[ApiExplorerSettings(IgnoreApi = true)]
public virtual void ToEntity(TEditModel model, TEntity entity)
{
}
[ApiExplorerSettings(IgnoreApi = true)]
public virtual void OnEdit(TEntity entity, TEditModel model)
{
}
[ApiExplorerSettings(IgnoreApi = true)]
public virtual void OnInserted(TEntity entity)
{
}
[ApiExplorerSettings(IgnoreApi = true)]
public virtual void OnUpdated(TEntity entity)
{
}
[ApiExplorerSettings(IgnoreApi = true)]
public virtual void OnDeleted(TEntity entity)
{
}
[ApiExplorerSettings(IgnoreApi = true)]
public override void OnActionExecuting(ActionExecutingContext context)
{
if (context == null)
@ -291,5 +311,11 @@ namespace Infrastructure.Web.Mvc
}
}
}
[ApiExplorerSettings(IgnoreApi = true)]
private IActionResult Result(object model)
{
return this.Request.Headers["accept"].ToString().Contains("json") ? Json(model) as IActionResult : View(model);
}
}
}

@ -11,6 +11,7 @@ using System.Linq;
namespace IoT.Shared.Areas.Admin.Controllers
{
[ApiExplorerSettings(IgnoreApi = false)]
[Authorize]
[Area(nameof(Admin))]
public class UserController : CrudController<User, PagedListModel<EditUserModel>, EditUserModel, EditUserModel>

@ -7,5 +7,6 @@
}
},
"AllowedHosts": "*",
"server.urls": "http://localhost:5001"
"server.urls": "http://localhost:5001",
"gateway": "http://localhost"
}
Loading…
Cancel
Save