Add Control to a Project
- 2 minutes to read
This topic describes how to build a Razor Pages and MVC Web Apps with the Spreadsheet component.
Before start, you should configure your Visual Studio project to use the Spreadsheet component.
#Configure Razor Pages Web Apps
If you use a Spreadsheet in a Razor Pages web app, it is necessary to add the following adjustments:
Disable the AntiForgeryToken option.
- To disable the AntiForgeryToken for an application, add the following code in the Program.cs file:
C#builder.Services.AddMvc().AddRazorPagesOptions(o => { o.Conventions.ConfigureFilter(new Microsoft.AspNetCore.Mvc.IgnoreAntiforgeryTokenAttribute()); });
- Add the following code to a page model to disable the AntiForgeryToken for a page:
C#[IgnoreAntiforgeryToken] public class IndexModel : PageModel { public void OnPost() { } }
Add the following code to the app.UseEndpoints function to access an MVC Controller action in Razor Pages.
C#app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); endpoints.MapControllerRoute(name: "default", pattern: "{controller=Spreadsheet}/{action=Index}/{id?}"); });
Declare a POST action handler in the SpreadsheetController controller.
C#public class SpreadsheetController : Controller { [HttpPost] [HttpGet] public IActionResult OnPostDocumentRequest() { return SpreadsheetRequestProcessor.GetResponse(HttpContext); } }
Use this handler in the DocumentRequestHandlerUrl(String) method as follows.
@(Html.DevExpress().Spreadsheet("spreadsheet") .DocumentRequestHandlerUrl(Url.Action("OnPostDocumentRequest", "Spreadsheet")) //... )
#Configure MVC Web Apps
Follow the steps below to add a Spreadsheet to an MVC web page:
Create a new controller or open an existing controller (for example, the HomeController.cs file), and add an action that handles document commands as shown below:
C#using DevExpress.AspNetCore.Spreadsheet; ... public class HomeController : Controller { [HttpPost] [HttpGet] public IActionResult DxDocRequest() { return SpreadsheetRequestProcessor.GetResponse(HttpContext); } /// ... }
Use the Spreadsheet helper to add a spreadsheet to a Razor file (.cshtml):
cshtml@(Html.DevExpress() .Spreadsheet("spreadsheet") .DocumentRequestHandlerUrl(Url.Action("DxDocRequest")))