You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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:

1. Create the Custom MVC Admin Page

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.cshtml

  • Build the custom controller
    Your controller should inherit from the standard MVC Controller, and use Sitefinity’s BackendPageRoute attribute 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-in BackendLayout.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>
    
2. Add a Top-Level Navigation Section

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 an Infrastructure folder 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 your Global.asax.cs file, 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).

3. Test Your Setup
  1. Compile your Visual Studio project and run your Sitefinity site locally (or deploy the changes to your server).
  2. Log into the Sitefinity backend with an admin account.
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:21:10