久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

用于單個路由的 ASP.NET Web API 自定義 IHttpControll

ASP.NET Web API custom IHttpControllerSelector for a single route(用于單個路由的 ASP.NET Web API 自定義 IHttpControllerSelector)
本文介紹了用于單個路由的 ASP.NET Web API 自定義 IHttpControllerSelector的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我需要一個自定義的 IHttpControllerSelector,它應該只應用于特定的路由.所有其他 web api 路由應使用 IHttpControllerSelector 的默認實現.

I need a custom IHttpControllerSelector which should be applied to a specific route only. All other web api routes should use the default implementation of IHttpControllerSelector.

在研究時,我發現以下代碼旨在在應用程序啟動時替換 IHttpControllerSelector,但它完全替換了默認控制器選擇器,這導致應用程序中的所有路由都使用我的自定義控制器選擇器:

While researching I found the following code that is meant to replace the IHttpControllerSelector at application start, but it replaces the default controller selector completely, which causes that all routes in the application use my custom controller selector:

config.Services.Replace(typeof(IHttpControllerSelector), 
                                                    new CustomControllerSelector(config));

有沒有辦法為單個路由配置 IHttpControllerSelector?

Is there a way to configure the IHttpControllerSelector for a single route?

推薦答案

您可以將每個路由消息處理程序分配給需要使用不同控制器選擇邏輯的路由.此處理程序將標記 HttpRequestMessage 與此請求需要區別對待的標志.

You can assign a per-route message handler to the route that needs to use a different controller selection logic. This handler would mark the HttpRequestMessage with a flag that this request needs to be treated differently.

然后簡單地使 CustomControllerSelectorDefaultHttpControllerSelector 繼承并檢查該標志:

Then simply make the CustomControllerSelector inherit from DefaultHttpControllerSelector and inspect that flag:

  • 如果已設置,請繼續使用您的自定義邏輯
  • 如果沒有設置,返回base(DefaultHttpControllerSelector)

代碼如下:

1) 消息處理程序,設置標志

1) message handler, setting the flag

public class RouteSpecificHandler : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        request.Properties["UseCustomSelector"] = true;
        return base.SendAsync(request, cancellationToken);
    }
}

2) 僅將每個路由消息處理程序分配給特定路由(不要為其他路由運行)

2) assigning per route message handler to the specific route only (do not run for other routes)

        config.Routes.MapHttpRoute(
            name: "MyRoute",
            routeTemplate: "api/dummy/{id}",
            defaults: new {controller = "Dummy", id = RouteParameter.Optional},
            constraints: null,
            handler: new RouteSpecificHandler { InnerHandler = new HttpControllerDispatcher(config) }
            );

3) 尊重標志的自定義選擇器:

3) custom selector respecting the flag:

public class CustomSelector : DefaultHttpControllerSelector
{
    public CustomSelector(HttpConfiguration configuration) : base(configuration)
    {
    }

    public override HttpControllerDescriptor SelectController(HttpRequestMessage request)
    {
        if (request.Properties.ContainsKey("UseCustomSelector") &&
            request.Properties["UseCustomSelector"] as bool? == true)
        {
            //your logic goes here
        }

        return base.SelectController(request);
    }
}

4) 注冊選擇器:

config.Services.Replace(typeof(IHttpControllerSelector), new CustomSelector(config));

編輯

如果您不希望從 DefaultHttpControllerSelector 繼承 - 然后直接實現 IHttpControllerSelector,而不是調用 base.SelectController(request) 保存舊選擇器作為類中的字段/屬性

edit

If you wish to not inherit from DefaultHttpControllerSelector - then implement IHttpControllerSelector directly, and instead of calling the base.SelectController(request) save the old selector as a field/property in your class

public class CustomSelector : IHttpControllerSelector
{
    private HttpConfiguration _config;

    public IHttpControllerSelector PreviousSelector {get; set;}

    public CustomSelector(HttpConfiguration configuration)
    {
         _config = configuration;
    }

    public override HttpControllerDescriptor SelectController(HttpRequestMessage request)
    {
        if (request.Properties.ContainsKey("UseCustomSelector") &&
            request.Properties["UseCustomSelector"] as bool? == true)
        {
            //your logic goes here
        }

        return PreviousSelector.SelectController(request);
    }
}

那就改注冊吧:

  var previousSelector = config.Services.GetService(typeof(IHttpControllerSelector)) as IHttpControllerSelector;
 config.Services.Replace(typeof(IHttpControllerSelector), new CustomSelector(config) { PreviousSelector = previousSelector});  

這篇關于用于單個路由的 ASP.NET Web API 自定義 IHttpControllerSelector的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進行身份驗證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權不起作用)
ASP Core Azure Active Directory Login use roles(ASP Core Azure Active Directory 登錄使用角色)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護進程或服務器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發技
.Net Core 2.0 - Get AAD access token to use with Microsoft Graph(.Net Core 2.0 - 獲取 AAD 訪問令牌以與 Microsoft Graph 一起使用)
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
主站蜘蛛池模板: 中文字幕亚洲精品 | 色综合一区二区三区 | 性色av一区二区三区 | 国产精品一区二区在线播放 | 国产一区在线视频 | 国产精品久久国产精品99 | 国产目拍亚洲精品99久久精品 | 日日操夜夜操天天操 | 亚洲视频在线看 | 欧美精品三区 | 一区二区久久 | 欧美日在线 | 一区二区三区视频在线免费观看 | 国内精品久久久久久久影视简单 | 久久久久久久91 | 91在线视频在线观看 | 国产高清在线观看 | 老外几下就让我高潮了 | 亚洲免费人成在线视频观看 | 亚洲欧美日韩精品久久亚洲区 | 欧美aaaaaaaa| 欧美日批 | 国产一区二区在线视频 | 久久亚洲欧美日韩精品专区 | 精品一区二区在线视频 | 美国av毛片 | 精品久久精品 | 激情在线视频网站 | 久久精品久久精品久久精品 | 成人a视频 | 欧美一区成人 | 亚洲导航深夜福利涩涩屋 | 男女污污网站 | 欧美日韩视频在线播放 | 国产精品久久久久久吹潮 | 亚洲福利免费 | 国产精品一区二区三区久久久 | 欧美一区二区三区在线观看视频 | 天天干天天爱天天 | 成人亚洲精品 | 日韩超碰在线 |