美文网首页
Asp.Net Core MVC 视图中如何使用依赖注入

Asp.Net Core MVC 视图中如何使用依赖注入

作者: 若能遇见 | 来源:发表于2021-01-05 15:32 被阅读0次

在视图中接收依赖注入

  视图文件中,使用@inject指令可接收来自依赖注入的对象实例。格式为:@inject<类型><变量名>

  • 如下
@page "/"
@inject  Microsoft.AspNetCore.Hosting.IHostingEnvironment envhost
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Demo</title>
</head>
<body>
    <div>
        当前应用程序:@envhost.ApplicationName
    </div>
    <div>
        运行环境:@envhost.EnvironmentName
    </div>
    <div>
        应用根目录:@envhost.ContentRootPath
    </div>
</body>
</html>

相关文章

网友评论

      本文标题:Asp.Net Core MVC 视图中如何使用依赖注入

      本文链接:https://www.haomeiwen.com/subject/uconoktx.html