src/Framework/Utils/Utils.Mvc/Helpers/MvcHelper.cs

(开头部分) 5KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Controllers;
using NetModular.Lib.Utils.Core.Attributes;

namespace NetModular.Lib.Utils.Mvc.Helpers
{
    /// <summary>
    /// MVC帮助类
    /// </summary>
    [Singleton]
    public class MvcHelper
    {
        private List<ControllerDescriptor> _controllerDescriptors;
        private List<ActionDescriptor> _actionDescriptors;
        private readonly ApplicationPartManager _partManager;
        private static readonly object LockObject = new object();

        public MvcHelper(ApplicationPartManager partManager)
        {
            _partManager = partManager;
        }

        /// <summary>
        /// 获取所有Controller,不包含抽象的类
        /// </summary>
        /// <returns></returns>
        public List<ControllerDescriptor> GetAllController()
        {
            lock (LockObject)
            {
                if (_controllerDescriptors != null && _controllerDescriptors.Any())
                    return _controllerDescriptors;

                _controllerDescriptors = new List<ControllerDescriptor>();

                var controllerFeature = new ControllerFeature();
                _partManager.PopulateFeature(controllerFeature);
                var controllerTypes = controllerFeature.Controllers.ToList();
                foreach (var typeInfo in controllerTypes)
                {
                    if (typeInfo.IsAbstract)
                        continue;

                    var controller = new ControllerDescriptor
                    {
                        Name = typeInfo.Name.Replace("Controller", ""),
                        TypeInfo = typeInfo
                    };

                    var areaAttr = (AreaAttribute)Attribute.GetCustomAttribute(typeInfo, typeof(AreaAttribute));
                    if (areaAttr != null)
                    {
                        controller.Area = areaAttr.RouteValue;
                    }
后面还有 114 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。