相关疑难解决方法(0)

asp.net Core mvc隐藏和排除Web Api控制器方法

我知道有ApiExplorerSettings属性

[ApiExplorerSettings(IgnoreApi = true)]
public async Task<IActionResult> MyMethod(int id)
Run Code Online (Sandbox Code Playgroud)

但这并不能阻止api的客户端调用端点方法.

我需要知道是否存在禁用端点的属性并且不允许请求.我想通过修改路由机制来避免这样做.

c# asp.net-core-mvc asp.net-core

7
推荐指数
1
解决办法
6726
查看次数

具有多个参数的 webapi 方法的自定义模型绑定器

我拥有的

我有一个具有以下方法的 api 控制器(ASP.NET Core MVC):

[HttpPost]
[Route("delete")]
public Task<ActionResult> SomeAction(Guid[] ids,  UserToken userToken, CancellationToken cancellationToken)
{
   ....
}
Run Code Online (Sandbox Code Playgroud)

我有一个自定义模型绑定器和绑定器提供程序:

public class UserTokenBinderProvider : IModelBinderProvider
{
    public IModelBinder GetBinder(ModelBinderProviderContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        if (context.Metadata.ModelType == typeof(UserToken))
        {
            return new BinderTypeModelBinder(typeof(UserTokenBinder));
        }

        return null;
    }
}

public class UserTokenBinder: IModelBinder
{
    public async Task BindModelAsync(ModelBindingContext bindingContext)
    {
        var token = await bindingContext.ActionContext.HttpContext.User.ToUserTokenAsync(CancellationToken.None);

        bindingContext.Result = ModelBindingResult.Success(token ?? UserToken.UnidentifiedUser);
    }
}
Run Code Online (Sandbox Code Playgroud)

将活页夹提供程序添加到服务:

services.AddMvc(options =>
{ …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-mvc asp.net-core

4
推荐指数
1
解决办法
2591
查看次数

标签 统计

asp.net-core ×2

asp.net-core-mvc ×2

c# ×2