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

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

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

問題描述

限時(shí)送ChatGPT賬號(hào)..

我需要一個(gè)自定義的 IHttpControllerSelector,它應(yīng)該只應(yīng)用于特定的路由.所有其他 web api 路由應(yīng)使用 IHttpControllerSelector 的默認(rèn)實(shí)現(xiàn).

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.

在研究時(shí),我發(fā)現(xiàn)以下代碼旨在在應(yīng)用程序啟動(dòng)時(shí)替換 IHttpControllerSelector,但它完全替換了默認(rèn)控制器選擇器,這導(dǎo)致應(yīng)用程序中的所有路由都使用我的自定義控制器選擇器:

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));

有沒有辦法為單個(gè)路由配置 IHttpControllerSelector?

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

推薦答案

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

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 繼承并檢查該標(biāo)志:

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

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

代碼如下:

1) 消息處理程序,設(shè)置標(biāo)志

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) 僅將每個(gè)路由消息處理程序分配給特定路由(不要為其他路由運(yùn)行)

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) 尊重標(biāo)志的自定義選擇器:

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 繼承 - 然后直接實(shí)現(xiàn) IHttpControllerSelector,而不是調(diào)用 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});  

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

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

相關(guān)文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進(jìn)行身份驗(yàn)證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權(quán)不起作用)
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(如何獲取守護(hù)進(jìn)程或服務(wù)器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發(fā)技
.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(異步調(diào)用時(shí) Azure KeyVault Active Directory AcquireTokenAsync 超時(shí))
主站蜘蛛池模板: av在线播放一区二区 | 黄a在线观看 | 麻豆精品久久久 | 大香网伊人 | 午夜久久| 玖玖视频网| 日韩视频精品在线 | 国产乱码精品一区二区三区忘忧草 | 一区二区亚洲 | 欧美mv日韩mv国产网站91进入 | 日本黄色大片免费 | 久久精品屋| av在线免费观看网站 | 久久精品国产99国产精品 | 偷拍亚洲色图 | 亚洲精品久久久久久国产精华液 | 国产精品99久久久久久人 | 久久久2o19精品 | 欧美日韩国产中文字幕 | 国产精品久久久亚洲 | 日韩1区2区 | 亚洲综合大片69999 | 欧美黑人激情 | 日本aⅴ中文字幕 | 一级少妇女片 | 亚洲午夜av久久乱码 | 久久久www | 国产精品国产成人国产三级 | 国产精品视频免费观看 | 亚洲国产精品久久 | 成人精品影院 | 午夜欧美一区二区三区在线播放 | 亚洲图片一区二区三区 | 亚洲国产精品一区二区久久 | 8x国产精品视频一区二区 | 久久婷婷av| 国产精品久久国产精品 | 成人免费看黄 | 日本中文字幕在线观看 | 免费一级欧美在线观看视频 | 亚洲人成网亚洲欧洲无码 |