我有一个字符串:
"super exemple of string key : text I want to keep - end of my string"
Run Code Online (Sandbox Code Playgroud)
我想保留在"key : "和之间的字符串" - ".我怎样才能做到这一点?我必须使用正则表达式,还是可以用其他方式进行?
如何获取Controller的所有操作列表?我搜索但找不到示例/答案.我看到一些推荐使用反射的例子,但我不知道如何.
这是我想要做的:
public List<string> ActionNames(string controllerName){
}
Run Code Online (Sandbox Code Playgroud) 如何在dotnet核心中查找其属性的所有控制器和操作?在.NET Framework中,我使用了以下代码:
public static List<string> GetControllerNames()
{
List<string> controllerNames = new List<string>();
GetSubClasses<Controller>().ForEach(type => controllerNames.Add(type.Name.Replace("Controller", "")));
return controllerNames;
}
public static List<string> ActionNames(string controllerName)
{
var types =
from a in AppDomain.CurrentDomain.GetAssemblies()
from t in a.GetTypes()
where typeof(IController).IsAssignableFrom(t) &&
string.Equals(controllerName + "Controller", t.Name, StringComparison.OrdinalIgnoreCase)
select t;
var controllerType = types.FirstOrDefault();
if (controllerType == null)
{
return Enumerable.Empty<string>().ToList();
}
return new ReflectedControllerDescriptor(controllerType)
.GetCanonicalActions().Select(x => x.ActionName).ToList();
}
Run Code Online (Sandbox Code Playgroud)
但它不适用于dotnet核心.
我有一个类在类级别定义数据注释.元数据类具有与之关联的自定义属性,以及通常的DisplayName,DisplayFormat等.
public class BaseMetaData
{
[DisplayName("Id")]
public object Id { get; set; }
[DisplayName("Selected")]
[ExportItem(Exclude = true)]
public object Selected { get; set; }
}
[MetadataType(typeof(BaseMetaData))]
public class BaseViewModel
{
public int Id { get; set; }
public bool Selected { get; set; }
Run Code Online (Sandbox Code Playgroud)
给定类型T,如何从元数据类中检索自定义属性?下面的尝试不起作用,因为元数据属性来自BaseViewModel而不是BaseMetaData类.
需要一般工作,即不能做typeof(BaseMetaData).GetProperty(e.PropertyName).想知道是否有从类中获取MetadataType的方法,那么它将使其成为可能.
var type = typeof (T);
var metaData = ModelMetadataProviders.Current.GetMetadataForType(null, type);
var propertMetaData = metaData.Properties
.Where(e =>
{
var attribute = type.GetProperty(e.PropertyName)
.GetCustomAttributes(typeof(ExportItemAttribute), false)
.FirstOrDefault() as ExportItemAttribute; …Run Code Online (Sandbox Code Playgroud) 在 .NET Core 3.1 和 .NET 5 中,我们进行了Xunit如下例所示的测试。它确保每个人Controller都有一个AuthorizeAttribute防止安全漏洞的方法。
将我们的 Web 项目升级到 ASP.NET Core 6 的最小托管模型时,不再需要Program和类。Startup一切正常,除了以下几点:
var types = typeof(Startup).Assembly.GetTypes();
Run Code Online (Sandbox Code Playgroud)
查看命名空间Example.Web,我也看不到任何可以从中加载程序集的类。如何Program.cs在 .NET 6 中加载程序集?
.NET 5 的示例:
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace Example.Web.Tests.ControllerTests
{
public class AuthorizeAttributeTest
{
[Fact]
public void ApiAndMVCControllersShouldHaveAuthorizeAttribute()
{
var controllers = GetChildTypes<ControllerBase>();
foreach (var controller in controllers)
{
var attribute = Attribute.GetCustomAttribute(controller, typeof(Microsoft.AspNetCore.Authorization.AuthorizeAttribute), true) as Microsoft.AspNetCore.Authorization.AuthorizeAttribute; …Run Code Online (Sandbox Code Playgroud) c# ×4
.net ×1
.net-6.0 ×1
action ×1
asp.net-core ×1
asp.net-mvc ×1
controller ×1
reflection ×1
regex ×1
string ×1
xunit ×1