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 theOnPostReadorOnGetReadhandler method in theIndex.cshtml.csfile. -
Using a query string
C#Url("/PathToPage?handler=HandlerName")For example,
Url("/Index?handler=Read")references theOnPostReadorOnGetReadhandler method in theIndexpage.
For more information on Razor Pages architecture and concepts, refer to the official Microsoft documentation.
Getting Started
To configure the CRUD operations in the FileManager within a Razor Pages application, follow the next steps:
-
Specify the
Read,Create,Update, andDestroyoptions of theDataSourceconfiguration. The URL in each of these options must refer to the method name in thePageModel.Razor@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") ) -
Add an
AntiForgeryTokenat the top of the page.Razor@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf @Html.AntiForgeryToken() -
Send the
AntiForgeryTokenwith 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> -
Within the
cshtml.csfile, add a handler method for each data operation.C#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.