1:
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.pclass = this; // 通过ViewBag把this指针传给视图,
return View();
}
public string f1(int i)
{
int a=i+100;
return a.ToString();
}
} 在视图文件就可以像下面一样调用: @{ Layout = null;
string s = ViewBag.pclass.f1(5);
}
2:
ASP.NET Core 获取控制器上的自定义属性
////// 参考代码Microsoft.AspNetCore.Mvc.Internal.AuthorizationApplicationModelProvider /// public class TestApplicationModelProvider : IApplicationModelProvider { public void OnProvidersExecuting(ApplicationModelProviderContext context) { //获取控制器上指定类型的属性 var ad = context.Result.Controllers.First().Attributes.OfType(); } public void OnProvidersExecuted(ApplicationModelProviderContext context) { } public int Order { get; } = -1000 + 10; }
注册:services.AddTransient<IApplicationModelProvider, TestApplicationModelProvider>();
3: