You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio 2013中能否在Razor视图中使用@inject关键字?

Answer to Your Dependency Injection in Views Question

Let's break this down clearly for you—you're running into a framework and IDE limitation here, but there are actionable options depending on whether you want to stick with your current setup or adopt the @inject feature natively.

Why @inject isn't working in your environment

The @inject keyword is a feature built specifically for ASP.NET Core MVC's Razor view engine. Your setup uses the older, traditional ASP.NET MVC built on .NET Framework 4.5.1, which doesn't include this functionality out of the box. On top of that, Visual Studio 2013 doesn't support development for ASP.NET Core at all—so even if you upgraded your .NET Framework version alone, you still wouldn't be able to use @inject in that IDE.

Option 1: Access dependencies without upgrading

If keeping VS2013 and .NET Framework 4.5.1 is a hard requirement, you can still access your services in views using these workarounds:

  • Pass dependencies from the controller (cleanest approach): Inject your service into the controller via constructor injection, then pass it to the view using ViewData, ViewBag, or a strongly-typed view model. Example:
    // Controller code
    public class HomeController : Controller
    {
        private readonly IMyService _myService;
        public HomeController(IMyService myService)
        {
            _myService = myService;
        }
        public ActionResult Index()
        {
            ViewBag.MyService = _myService;
            return View();
        }
    }
    
    <!-- View code -->
    @{ var myService = ViewBag.MyService as IMyService; }
    <!-- Use myService for your logic here -->
    
  • Resolve directly via DependencyResolver: While not ideal (it couples your view to the DI container and makes testing harder), you can pull services directly in the view:
    @{ var myService = DependencyResolver.Current.GetService<IMyService>(); }
    
    Use this sparingly, as it reduces the maintainability of your views.

Option 2: Upgrade to use @inject natively

If you want to leverage the @inject keyword as intended, you'll need two key upgrades:

  1. Visual Studio version: VS2013 doesn't support ASP.NET Core development. You'll need to move to Visual Studio 2017 or later (VS2019/2022 are recommended for better compatibility and newer features).
  2. Switch to ASP.NET Core: You can either migrate your existing project to ASP.NET Core (which can run on .NET Framework 4.6.1+ or modern .NET versions like .NET 6/7) or start fresh with an ASP.NET Core project. Once you're on this stack, @inject will work natively in your Razor views, just like the official documentation describes.

Final Takeaway

If staying on your current setup is non-negotiable, use the controller-to-view passing method (preferred) or DependencyResolver as a fallback. If you can upgrade, moving to ASP.NET Core with a compatible VS version will give you native access to @inject and a host of other modern MVC features.

内容的提问来源于stack exchange,提问作者BVernon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:30:42