如何在Visual Studio中创建Sitefinity自定义MVC后端页面及顶部导航项
Absolutely! You can totally build a custom MVC admin page in your Visual Studio Sitefinity project—full control over your controller, matching the backend's styling, and even add a new top-level navigation section right alongside Dashboard, Pages, etc. Let’s walk through how to do this step by step:
To keep things aligned with Sitefinity’s backend styling and maintain full controller control, follow these steps:
Set up your MVC folder structure
In your Sitefinity project, create a dedicated folder structure for your admin controller and views, e.g.:~/Controllers/Admin/CustomAdminController.cs~/Views/Admin/Index.cshtmlBuild the custom controller
Your controller should inherit from the standard MVC Controller, and use Sitefinity’sBackendPageRouteattribute to map it to a backend-friendly URL. Add permission checks to ensure only authorized users can access it:using System.Web.Mvc; using Telerik.Sitefinity.Web.Mvc.Controllers; using Telerik.Sitefinity.Security; namespace YourSitefinityProject.Controllers.Admin { [BackendPageRoute(Url = "admin/custom-tools")] [Authorize(Roles = "Administrators")] // Restrict to admin roles public class CustomAdminController : Controller { public ActionResult Index() { // Add your custom business logic here return View(); } } }Create the view with backend styling
The key to matching the backend’s look is using Sitefinity’s built-inBackendLayout.cshtml. Add this to your view to inherit the standard backend header, sidebar, and styling:@{ Layout = "~/Views/Shared/BackendLayout.cshtml"; ViewBag.Title = "Custom Admin Tools"; // Sets the page title in the backend } <!-- Your custom content goes here, using Sitefinity's backend CSS classes --> <div class="sfContentBlock"> <h1 class="sfHeading">Welcome to Custom Admin Tools</h1> <p>This page uses Sitefinity's native backend styling and runs your custom MVC logic.</p> <!-- Add your custom UI elements here --> </div>
To add a new section to the backend’s top navigation bar (like Dashboard or Pages), you’ll need to modify Sitefinity’s navigation configuration via code:
Create a navigation initializer class
This class will register your new navigation section when the site boots up. Place it in anInfrastructurefolder or similar:using Telerik.Sitefinity.Abstractions; using Telerik.Sitefinity.Web.Configuration; namespace YourSitefinityProject.Infrastructure { public class CustomNavigationInitializer : IInitializer { public void Initialize() { Bootstrapper.Initialized += Bootstrapper_Initialized; } private void Bootstrapper_Initialized(object sender, ExecutedEventArgs e) { // Only run once after the site is fully bootstrapped if (e.CommandName != "Bootstrapped") return; var navConfig = Config.Get<NavigationConfig>(); // Avoid duplicate sections on repeated app starts if (navConfig.BackendNavigation.Sections["CustomAdminSection"] != null) return; // Create the new navigation section var customSection = new SectionElement(navConfig.BackendNavigation.Sections) { Name = "CustomAdminSection", Title = "Custom Tools", // Text that appears in the top nav Description = "Access custom administrative tools", Url = "~/admin/custom-tools", // Link to your MVC page CssClass = "sfCustomToolsIcon", // Optional: for a custom icon Permissions = new[] { "Administrators" } // Restrict visibility to admins }; // Add the section to the backend navigation navConfig.BackendNavigation.Sections.Add(customSection); // Save the configuration changes ConfigManager.GetManager().SaveSection(navConfig); } } }Register the initializer
In yourGlobal.asax.csfile, register the initializer during the application start:protected void Application_Start(object sender, EventArgs e) { // Register your custom navigation initializer new CustomNavigationInitializer().Initialize(); }Optional: Add a custom icon
To make your navigation section stand out, add a custom CSS class for the icon. You can use Font Awesome icons (Sitefinity includes them by default):/* Add this to a custom backend CSS file, e.g., ~/Styles/CustomBackend.css */ .sfCustomToolsIcon:before { content: "\f085"; /* Adjust this to your desired icon code */ font-family: "FontAwesome"; }Then reference this CSS in Sitefinity’s backend settings (under Administration > Settings > Advanced > Appearance > Backend CSS).
- Compile your Visual Studio project and run your Sitefinity site locally (or deploy the changes to your server).
- Log into the Sitefinity backend with an admin account.
- You’ll see your new "Custom Tools" section in the top navigation bar—click it to access your custom MVC page, which will match the backend’s styling perfectly and run your controller logic.
内容的提问来源于stack exchange,提问作者Jake Householder

