路由参数,自定义模型绑定器或动作过滤器?

Ben*_*ter 5 asp.net-mvc modelbinders action-filter asp.net-mvc-3

我们的ASP.NET MVC应用程序允许经过身份验证的用户管理链接到其帐户的一个或多个"站点".

我们的网址是高度猜测的,因为我们在网址而不是Id中使用网站友好名称,例如:

/sites/mysite/
/sites/mysite/settings

/sites/mysite/blog/posts
/sites/mysite/pages/create
Run Code Online (Sandbox Code Playgroud)

如您所见,我们需要在多个路线中访问站点名称.

我们需要为所有这些操作执行相同的行为:

  1. 当前帐户中查找具有给定标识符的站点
  2. 如果返回的站点为null,则返回404(或自定义视图)
  3. 如果站点非null(有效),我们可以继续执行该操作

我们始终可以通过ISiteContext对象使用当前帐户.以下是我如何使用普通路由参数实现上述所有操作并直接在我的操作中执行查询:

private readonly ISiteContext siteContext;
private readonly IRepository<Site> siteRepository;

public SitesController(ISiteContext siteContext, IRepository<Site> siteRepository)
{
    this.siteContext = siteContext;
    this.siteRepository = siteRepository;
}

[HttpGet]
public ActionResult Details(string id)
{
    var site =
        siteRepository.Get(
            s => s.Account == siteContext.Account && s.SystemName == id
        );

    if (site == null)
        return HttpNotFound();

    return Content("Viewing details for site " + site.Name);
}
Run Code Online (Sandbox Code Playgroud)

这不是太糟糕,但我需要在20个左右的动作方法上做这个,所以想尽可能保持干燥.

我没有做过自定义模型粘合剂,所以我想知道这是否是一个更适合他们的工作.一个关键要求是我可以将我的依赖项注入模型绑定器(对于ISiteContext和IRepository - 如果需要,我可以回退到DependencyResolver).

非常感谢,

更新

下面是工作代码,使用自定义模型绑定器和操作筛选器.我仍然不确定我对此的感受是因为

  1. 我应该从模型绑定器上击中我的数据库吗?
  2. 我实际上可以从动作过滤器中检索对象和空验证.哪个更好?

型号粘合剂:

public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
    if (!controllerContext.RouteData.Values.ContainsKey("siteid"))
        return null;

    var siteId = controllerContext.RouteData.GetRequiredString("siteid");

    var site =
        siteRepository.Get(
            s => s.Account == siteContext.Account && s.SystemName == siteId
        );

    return site;
}
Run Code Online (Sandbox Code Playgroud)

动作过滤器:

public class ValidateSiteAttribute : ActionFilterAttribute
{       
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {           
        var site = filterContext.ActionParameters["site"];

        if (site == null || site.GetType() != typeof(Site))
            filterContext.Result = new HttpNotFoundResult();

        base.OnActionExecuting(filterContext);
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器动作:

[HttpGet]
[ValidateSite]
public ActionResult Settings(Site site)
{
    var blog = site.GetFeature<BlogFeature>();
    var settings = settingsProvider.GetSettings<BlogSettings>(blog.Id);

    return View(settings);
}

[HttpPost]
[ValidateSite]
[UnitOfWork]
public ActionResult Settings(Site site, BlogSettings settings)
{
    if (ModelState.IsValid)
    {
        var blog = site.GetFeature<BlogFeature>();
        settingsProvider.SaveSettings(settings, blog.Id);
        return RedirectToAction("Settings");
    }

    return View(settings);
}
Run Code Online (Sandbox Code Playgroud)

Cha*_*ino 1

这听起来绝对像是动作过滤器的工作。您可以使用动作过滤器进行 DI,这不是问题。

所以,是的,只需将您现有的功能转换为操作过滤器,然后将其应用于您继承的每个操作或控制器或基本控制器。

我不太清楚您的网站是如何工作的,但您可以使用全局操作过滤器来检查特定路由值(例如“SiteName”)是否存在。如果该路由值存在,则意味着您需要继续检查该站点是否存在......