# FileManager in Razor Pages

This article describes how to seamlessly integrate and configure the Telerik UI FileManager for ASP.NET Core in Razor Pages applications.

## Referencing Handler Methods in Razor Pages

Razor Pages is an alternative to the MVC pattern that makes page-focused coding easier and more productive. This approach consists of a `cshtml` file and a `cshtml.cs` file (by convention, the two files have the same name).

The `cshtml.cs` file, known as the `PageModel`, contains handler methods that respond to HTTP requests. These methods are prefixed with `On` followed by the HTTP verb (for example, `OnGet`, `OnPost`, `OnPostRead`, `OnPostCreate`).

Handler methods declared in a `PageModel` can be referenced from any Razor Page using one of the following URL patterns:

* Using `Url.Page()`

  ```C#
  Url.Page("PageName", "HandlerName")
  // OR
  Url.Page("/FolderName/PageName", "HandlerName")
  ```

  For example, `Url.Page("Index", "Read")` references the `OnPostRead` or `OnGetRead` handler method in the `Index.cshtml.cs` file.

* Using a query string

  ```C#
  Url("/PathToPage?handler=HandlerName")
  ```

  For example, `Url("/Index?handler=Read")` references the `OnPostRead` or `OnGetRead` handler method in the `Index` page.

For more information on Razor Pages architecture and concepts, refer to [the official Microsoft documentation](https://learn.microsoft.com/en-us/aspnet/core/razor-pages/).

## Getting Started

To configure the CRUD operations in the FileManager within a Razor Pages application, follow the next steps:

1. Specify the `Read`, `Create`, `Update`, and `Destroy` options of the `DataSource` configuration. The URL in each of these options must refer to the method name in the `PageModel`.

   ```Razor HtmlHelper
       @page
       @model IndexModel

       @(Html.Kendo().FileManager()
           .Name("filemanager")
           .DataSource(ds =>
               {
                   ds.Read(operation => operation
                   .Type(HttpVerbs.Post)
                   .Url(Url.Page("Index", "FileManagerRead"))
                   .Data("forgeryToken")
               );
               ds.Destroy(operation => operation
                   .Type(HttpVerbs.Post)
                   .Url(Url.Page("Index", "FileManagerDestroy"))
                   .Data("forgeryToken")
               );
               ds.Create(operation => operation
                   .Type(HttpVerbs.Post)
                   .Url(Url.Page("Index", "FileManagerCreate"))
                   .Data("forgeryToken")
               );
               ds.Update(operation => operation
                   .Type(HttpVerbs.Post)
                   .Url(Url.Page("Index", "FileManagerUpdate"))
                   .Data("forgeryToken")
               );
           })
           .UploadUrl("/Index?handler=FileManagerUpload")
       )
   ```

   ```Razor TagHelper
       @page
       @model IndexModel

       <kendo-filemanager name="filemanager" upload-url="@Url.Page("Index", "FileManagerUpload")">
           <filemanager-datasource>
               <transport>
                   <read url="@Url.Page("Index", "FileManagerRead")" data="forgeryToken" type="POST" />
                   <destroy url="@Url.Page("Index", "FileManagerDestroy")" data="forgeryToken" type="POST" />
                   <create url="@Url.Page("Index", "FileManagerCreate")" data="forgeryToken" type="POST" />
                   <update url="@Url.Page("Index", "FileManagerUpdate")" data="forgeryToken" type="POST" />
               </transport>
           </filemanager-datasource>
       </kendo-filemanager>
   ```

2. Add an `AntiForgeryToken` at the top of the page.

   ```Razor
       @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
       @Html.AntiForgeryToken()
   ```

3. Send the `AntiForgeryToken` with the Read request.

   ```JavaScript
       <script>
           function forgeryToken() {
               return kendo.antiForgeryTokens();
           }
       </script>
   ```

   Additional parameters can also be supplied.

   ```JavaScript
       <script>
           function forgeryToken() {
               return {
                   __RequestVerificationToken: kendo.antiForgeryTokens().__RequestVerificationToken,
                   additionalParameter: "test"
               }
           }
       </script>
   ```

4. Within the `cshtml.cs` file, add a handler method for each data operation.

   ```C# Index.cshtml.cs
   public class IndexModel : PageModel
   {
       public IndexModel(IWebHostEnvironment hostingEnvironment)
       {
           HostingEnvironment = hostingEnvironment;
           directoryBrowser = new FileContentBrowser();
       }

       protected readonly IWebHostEnvironment HostingEnvironment;
       private readonly FileContentBrowser directoryBrowser;

       public virtual JsonResult OnPostFileManagerRead(string target)
       {
           var path = NormalizePath(target);
           if (Authorize(path))
           {
               try
               {
                   var files = directoryBrowser.GetFiles(path, Filter);
                   var directories = directoryBrowser.GetDirectories(path);
                   var result = files.Concat(directories).Select(VirtualizePath);
                   return new JsonResult(result.ToArray());
               }
               catch (DirectoryNotFoundException)
               {
                   throw new Exception("File Not Found");
               }
           }
           throw new Exception("Forbidden");
       }

       public virtual ActionResult OnPostFileManagerCreate(string target, FileManagerEntry entry)
       {
           FileManagerEntry newEntry;
           if (!Authorize(NormalizePath(target)))
           {
               throw new Exception("Forbidden");
           }

           if (String.IsNullOrEmpty(entry.Path))
           {
               newEntry = CreateNewFolder(target, entry);
           }
           else
           {
               newEntry = CopyEntry(target, entry);
           }
           return new JsonResult(VirtualizePath(newEntry));
       }

       public virtual ActionResult OnPostFileManagerDestroy(FileManagerEntry entry)
       {
           var path = NormalizePath(entry.Path);
           if (!string.IsNullOrEmpty(path))
           {
               if (entry.IsDirectory)
               {
                   DeleteDirectory(path);
               }
               else
               {
                   DeleteFile(path);
               }
               return new JsonResult(new object[0]);
           }
           throw new Exception("File Not Found");
       }

       public virtual ActionResult OnPostFileManagerUpdate(string target, FileManagerEntry entry)
       {
           FileManagerEntry newEntry;
           if (!Authorize(NormalizePath(entry.Path)) && !Authorize(NormalizePath(target)))
           {
               throw new Exception("Forbidden");
           }
           newEntry = RenameEntry(entry);
           return new JsonResult(VirtualizePath(newEntry));
       }
   }
   ```

For the complete project, refer to the [FileManager in Razor Pages example](https://github.com/telerik/ui-for-aspnet-core-examples/blob/master/Telerik.Examples.RazorPages/Telerik.Examples.RazorPages/Pages/FileManager/FileManagerIndex.cshtml).

## See Also

* [Using Telerik UI for ASP.NET Core in Razor Pages](https://docs.telerik.com/aspnet-core/getting-started/razor-pages#using-telerik-ui-for-aspnet-core-in-razor-pages)
* [Client-Side API of the FileManager](https://docs.telerik.com/kendo-ui/api/ui/filemanager)
* [Server-Side HtmlHelper API of the FileManager](https://www.telerik.com/aspnet-core-ui/documentation/api/filemanager)
* [Server-Side TagHelper API of the FileManager](https://www.telerik.com/aspnet-core-ui/documentation/api/taghelpers/filemanager)
* [Knowledge Base Section](https://www.telerik.com/aspnet-core-ui/documentation/knowledge-base)
