BaseController ASPCore MVC [英] BaseController ASPCore MVC

查看:61
本文介绍了BaseController ASPCore MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个BaseController Action方法,该方法在用于将View呈现给Razor之前用于修改View的Controller类的Action之前或之后被调用.

I want to implement a BaseController Action method that gets called either before or after the Action of the Controller class used to modify a View before its renders to Razor.

public class BaseViewModel
{
    public BaseViewModel() {}
    public string Property1 {get; set;}
}

public class ViewModel : BaseViewModel
{
    public ViewModel () : base() {}
    // Some View Methods
}

public class BaseController
{
    public BaseController();

    // here is where I want to put code to intercept the call to the 
    // AccountController when any action is performed and write to the
    // Property1  of the Base class of the ViewModel  class.
}


public class AccountController : BaseController
{
    public AccountController() : base() {}

    public IActionResult About()
    { 
       return(new ViewModel());
    }
}

我不知道该怎么做,或者什至可以实现.我可以得到一些反馈或建议吗?

I don't know how to do what I'm trying to explain, or if its even achievable. Could I get some feedback or recommendations?

BaseViewModel将包含对所有页面(如Culture,Title等)更通用的属性,这些属性与ViewModel完全无关,但可能在View上呈现.

The BaseViewModel will contains properties that are more generic to all pages like Culture, Title etc and not related to the ViewModel at all but perhaps renderded on the View.

推荐答案

您应该能够覆盖OnActionExecuting:

You should be able to override OnActionExecuting:

// Custom Base controller.
public class BaseController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // Do whatever here...
    }
}

// Account controller.
public class AccountController : BaseController
{
    // Action methods here...
}

这篇关于BaseController ASPCore MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆