title: How to check if view component exists before rendering in razor view in ASP.NET Core
body: <p>ASP.NET Core has the <a href="https://docs.asp.net/en/latest/mvc/views/view-components.html" rel="nofollow">View Components</a> mechanism for reusing parts of views. You can include the view component in a razor template file using <code>Component.InvokeAsync</code> call:</p>
  
  <pre><code>@await Component.InvokeAsync("MyComponent", new { data = 1 })
  </code></pre>
  
  <p>If view component with given name doesn't exist an <code>InvalidOperationException</code> exception is thrown.</p>
  
  <pre><code>InvalidOperationException: A view component named 'MyComponent' could not be found.
  </code></pre>
  
  <p>I would like to know how can i check if view component exist before rendering in razor view. Ideally something like:</p>
  
  <pre><code>@if (Component.Exists("MyComponent")
  {
      @await Component.InvokeAsync("MyComponent", new { data = 1 })
  }
  else
  {
      &lt;p&gt;Component not found&lt;/p&gt;
  }
  </code></pre>