说我有这门课:
[AttributeUsage(AttributeTargets.Method)]
public class MyAttribute : Attribute
{
public MyAttribute()
{
// Do stuff
}
~MyAttribute()
{
// When is this called? When the function ends? Whenever the GC feels like?
}
}
Run Code Online (Sandbox Code Playgroud) 我想创建一个自定义 AuthorizeAttribute 类,如下所述:Override Authorize Attribute in ASP.NET MVC。
诀窍是,用户是否被授权不仅取决于他正在执行的方法,还取决于参数:
[MyCustomAuthorize(id)]
[HttpGet]
public ActionResult File(Guid id)
{
}
Run Code Online (Sandbox Code Playgroud)
基本上,用户将被授权查看某些文件,但不能查看其他文件,因此我想将 id 传递给我的自定义授权属性。但我似乎无法这样做,因为名称“id”不在范围内。
我知道我可以执行逻辑来确保用户可以在方法开始时访问该文件,但是我有几种方法都需要完成此操作,如果我可以将其作为[授权] 属性。
编写自定义代码以使用 wordpress 数据库创建产品详细信息页面。
我已经显示了产品标题、描述、价格、库存等,并且被困在产品属性上。在数据库中,_product_attributes以序列化的方式存储在数据库的wp_postmeta表中。而且我无法从中解开属性。但我发现,每个属性值及其自己的价格都存储在 wp_postmeta 中的其他 post_id 中。
例如,post_id=55 的产品具有属性名称“尺寸值”,其值为 14 和 18,价格为 300 和 350,在 post_id=110,111 中显示为属性值和价格

背后有什么公式吗?有什么想法可以找到这个产品属性值和相应的价格值吗?
这是一个用于桌面的 C# WPF 应用程序。我创建了一个自定义属性类,但在调试中它永远不会进入那里。我需要在这个类中添加其他东西吗?
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class Authentication : Attribute, IPrincipal
{
public Authentication(string id)
{
throw new NotImplementedException();
}
public IIdentity Identity { get; private set; }
public bool IsInRole(string role)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
方法是:
[Authentication(SecurityConstants.DataEntrySupervisor)]
[Authentication(SecurityConstants.DataVerificationClerk)]
public void Submit(EventObject eventObject)
{//////////}
Run Code Online (Sandbox Code Playgroud) 我有一个这样的动作:
[HttpGet]
[Route("~/books/{id:int:min(1)}/{slug?}")]
public ActionResult Book(int? id, string slug)
{
if (slug == null)
{
slug = "awesome-book";
return RedirectToAction("Book", new { id, slug });
}
etc.
}
Run Code Online (Sandbox Code Playgroud)
问题是新路由的生成方式类似于“ books/1?slug=awesome-book ”,这不是我想要的,而是“ books/1/awesome-book ”。如何正确设置滑块?
我有一个 ASP.NET MVC Web 应用程序,我正在尝试改进其错误处理功能。我阅读了 HandleErrorAttribute 基类,并希望将它与我的控制器类一起使用。但是,当我从使用该属性修饰的控制器类之一中的方法抛出测试异常时,不会触发派生类中的OnException覆盖。我看到的不是我的派生错误处理类的 OnException 方法被触发,而是 XML 格式的通用错误消息,并且在我的应用程序中的任何地方都找不到通用错误消息:
<Error>
<Message>An error has occurred.</Message>
</Error>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
请注意,我尝试将以下自定义错误元素添加到我的web.config文件中的system.web元素,如此 SO 帖子所示,但它没有帮助:
ASP.net MVC [HandleError] 没有捕获异常
<customErrors mode="On" defaultRedirect="Error" />
Run Code Online (Sandbox Code Playgroud)
这是我的 HandleErrorAttribute 派生类:
public class HandleControllerErrors : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
// Breakpoint set here never hit.
Exception ex = filterContext.Exception;
filterContext.ExceptionHandled = true;
var model = new HandleErrorInfo(filterContext.Exception, "Controller", "Action");
filterContext.Result = new ViewResult() …Run Code Online (Sandbox Code Playgroud) 如何在 ASP.NET 中的自定义授权属性中获取引用数据,如 IP 地址和 apiKey。在下面的代码中,我想获取上面提到的数据:
[AttributeUsage(AttributeTargets.Method)]
public class Auth : AuthorizeAttribute
{
protected override bool IsAuthorized(HttpActionContext actionContext)
{
var cnt = HttpContext.Current;
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
在 HttpContext.Current 中,我可以获得 url 引用,但它没有我需要的所有数据。
如果这个问题不清楚或不完整,请告诉我。
更新:对于那些需要知道的人,下面的代码显示了如何。
[AttributeUsage(AttributeTargets.Method)]
public class Auth : AuthorizeAttribute
{
protected override bool IsAuthorized(HttpActionContext actionContext)
{
var cnt = HttpContext.Current;
int id = Int32.Parse(cnt.Request.Params["id"]);
string apiKey = cnt.Request.Params["apiKey"];
string IP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
return true;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在为我的 Angular 组件提供属性。我所说的属性是指您放入 HTML 中而不指定属性值的切换样式布尔属性:
与输入相反(这不是我想要的):
如果compact存在,则组件应将其值强制转换为布尔值:
<my-component compact="compact">// <= 真<my-component compact="true>// <= 真<my-component compact="false">// <= 正确?如果compact没有值则映射为 true:
<my-component compact>// <= 真如果compact遗漏了:
<my-component>// <= 假但是,当我将其保留为没有属性值时,组件会看到 null,因此即使我在构造函数中提供了默认值,我也无法区分情况 #4 和 #5。
constructor(
@Attribute('compact') public compact,
) {
console.log(this.compact) // `null` for <my-component compact>
}
Run Code Online (Sandbox Code Playgroud)
我该怎么办?属性装饰器单独可以吗
我对测量执行特定方法所需的时间很感兴趣。
我认为使用自定义属性而不是乱丢垃圾方法来启动/停止秒表并发送到记录器会非常方便。如果我可以使用属性来装饰有问题的方法,那将非常方便!
我能够按照本文创建自定义属性:https : //docs.microsoft.com/en-us/dotnet/standard/attributes/writing-custom-attributes
像这样:
public class MonitorExecutionTime : Attribute
{
private Stopwatch measureExecution;
// Start measuring on instantiation
public MonitorExecutionTime()
{
measureExecution = new Stopwatch();
measureExecution.Start();
}
// how do I hook into end invoke?
public MethodHasEnded()
{
measureExecution.Stop();
TimeSpan timeSpan = measureExecution.Elapsed;
Console.WriteLine("Time: {0}h {1}m {2}s {3}ms", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds);
}
}
Run Code Online (Sandbox Code Playgroud)
但我不确定如何“捕获”正在调用和结束执行的调用点,以便启动秒表和停止秒表(测量时间并记录它)。
有没有人在 .net 核心应用程序中采用这种方法?在此先感谢您的指点!
logging stopwatch custom-attributes azure-application-insights .net-core
在这里,我花了一些时间来了解有关C#的更多信息,因此我决定研究自定义属性,并且在分配给Enums时发现它们非常有用.
所以我为Enum编写了一些扩展方法来很容易地检索这些属性,比如:DemoEnum.Value1.GetAttribute<EnumNote>().
过了一会儿,我想如果每个自定义属性都有对它所分配的枚举的引用,这将是一个不错的主意.我想是个不错的主意,所以我继续这样做:
首先,我EnumAttribute为自定义属性编写了一个基System.Attribute类,当然继承了该类.这个基类只是第一个草图,我打算将它扩展为特别适合它将接收的每种类型的枚举,但到目前为止这已足够.
public class EnumAttribute : Attribute
{
public EnumInfo Enum { get; internal set; }
public EnumAttribute(Enum Enum)
{
this.Enum = new EnumInfo(Enum);
}
public class EnumInfo
{
private Enum _value;
private Type _type;
private FieldInfo _details;
public Enum Value { get { return _value; } }
public Type Type { get { return _type; } }
public FieldInfo Details { get { return _details; } }
public EnumInfo(Enum value)
{ …Run Code Online (Sandbox Code Playgroud)