ActionExecutingContext 获取特性上的注释
时间: 2024-03-06 20:46:59 浏览: 17
可以通过以下代码获取特性上的注释:
```
var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
var actionAttributes = actionDescriptor.MethodInfo.GetCustomAttributes(inherit: true);
var myAttribute = actionAttributes.FirstOrDefault(a => a.GetType() == typeof(MyAttribute)) as MyAttribute;
if (myAttribute != null)
{
var myAttributeComment = myAttribute.Comment;
// use the comment here
}
```
其中 `MyAttribute` 是自定义的特性类,`Comment` 是注释属性。
相关问题
.net core项目如何在过滤器中获取IP地址
在 .NET Core 项目中,可以通过使用内置的 IHttpContextAccessor 接口来获取客户端的 IP 地址。下面是一个获取 IP 地址的例子:
1. 首先,在 Startup.cs 文件的 ConfigureServices 方法中添加以下代码,以便注册 IHttpContextAccessor 接口:
```
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
```
2. 接下来,在需要获取 IP 地址的过滤器中注入 IHttpContextAccessor 接口,然后通过 HttpContext 对象获取 IP 地址,例如:
```
public class MyFilter : IActionFilter
{
private readonly IHttpContextAccessor _httpContextAccessor;
public MyFilter(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public void OnActionExecuting(ActionExecutingContext context)
{
var ipAddress = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
// do something with the IP address
}
public void OnActionExecuted(ActionExecutedContext context)
{
// do something after the action method executes
}
}
```
在上面的例子中,我们通过注入 IHttpContextAccessor 接口来获取当前 HttpContext 对象,然后使用 Connection.RemoteIpAddress 属性获取客户端的 IP 地址。注意,RemoteIpAddress 属性返回的是一个 IPAddress 对象,需要将其转换为字符串才能获取 IP 地址。
C# OnActionExecuting 跳过
如果你想在 ASP.NET Core 中跳过 `OnActionExecuting` 方法不执行它的代码,可以使用 `SkipOnActionExecuting` 特性。
首先,在你的控制器类上添加 `[SkipOnActionExecuting]` 特性,如下所示:
```csharp
[SkipOnActionExecuting]
public class MyController : Controller
{
// ...
}
```
然后,在你的 `OnActionExecuting` 方法中,检查控制器类上是否有 `SkipOnActionExecuting` 特性。如果有,就直接返回。如果没有,就执行正常的逻辑,如下所示:
```csharp
public override void OnActionExecuting(ActionExecutingContext context)
{
if (context.Controller.GetType().GetCustomAttribute<SkipOnActionExecutingAttribute>() != null)
{
return;
}
// Normal logic here...
}
```
这样,当你在某个具体的控制器类上添加了 `SkipOnActionExecuting` 特性时,该控制器类的 `OnActionExecuting` 方法就会被跳过。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)