---
title: "How to check file exist or not in MVC razor view?"  
description: "How to check file exist or not in MVC razor view?"  
author: "Anonymous User"  
published: 2017-12-26  
updated: 2017-12-26  
canonical: https://www.mindstick.com/forum/34415/how-to-check-file-exist-or-not-in-mvc-razor-view  
category: "c#"  
tags: ["asp.net mvc", "mvc", "mvc4", "razor"]  
reading_time: 2 minutes  

---

# How to check file exist or not in MVC razor view?

Hi [Expert](https://www.mindstick.com/articles/13120/an-expert-financial-advice-will-improve-your-finances),

I want to [check file](https://www.mindstick.com/forum/12820/how-to-check-file-format) existence in 'if' [condition](https://www.mindstick.com/forum/12711/select-query-with-and-condition) of my [application](https://www.mindstick.com/articles/12824/calculator-application-in-android) [razor view](https://www.mindstick.com/forum/155820/how-to-generate-textbox-control-using-htmlhelper-in-razor-view).

Here is my [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii),

```
<span class="qa-q-item-avatar">
                            <a href="@Url.Action("user", "account", new { user = @item.UserName.Replace(" ", "-"), @id = item.UserID })" class="qa-avatar-link">
  if(fileExist){//here we want to check file existence
  <img src="https://www.mindstick.com//Content/img/User Image/@(item.UserID).png" width="30" height="30" class="qa-avatar-image">
  }
  else
  {
  other wise show default image
  }
        </a>
</span>
```

Please give me the solution to accomplish my task.

## Replies

### Reply by Hemant Patel

Hi, Aarav

I analyzed your code,

Please follow the line of code to achieve your task.

```
<span class="qa-q-item-avatar">
                           <a href="@Url.Action("user", "account", new { user = @item.UserName.Replace(" ", "-"), @id = item.UserID })" class="qa-avatar-link">
                                @if (File.Exists(@"FilePath"))
                                {
                                    <img src="https://www.mindstick.com//Content/img/User Image/@(item.UserID).png" width="30" height="30" class="qa-avatar-image">
                                }
                                else
                                {
                                    Default Image Path                                }
                           </a>
                       </span>
```

## *I hope it's informative for you...*

\


---

Original Source: https://www.mindstick.com/forum/34415/how-to-check-file-exist-or-not-in-mvc-razor-view

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
