小编Nie*_*den的帖子

如何在 ASP.NET 中执行之前拦截 api 中的 GET 请求?

我试图弄清楚如何在 .NET 框架中执行之前拦截 GET 调用。

我创建了 2 个应用程序:一个前端(调用 API 并用它发送自定义 HTTP 标头)和一个后端 API:

前端调用API的方法:

[HttpGet]
    public async Task<ActionResult> getCall()
    {
        string url = "http://localhost:54857/";
        string customerApi = "2";

        using (var client = new HttpClient())
        {
            //get logged in userID
            HttpContext context = System.Web.HttpContext.Current;
            string sessionID = context.Session["userID"].ToString();

            //Create request and add headers
            client.BaseAddress = new Uri(url);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //Custom header
            client.DefaultRequestHeaders.Add("loggedInUser", sessionID);

            //Response
            HttpResponseMessage response = await client.GetAsync(customerApi);
            if (response.IsSuccessStatusCode)
            {
                string jsondata = await response.Content.ReadAsStringAsync();
                return Content(jsondata, "application/json"); …
Run Code Online (Sandbox Code Playgroud)

c# api asp.net-mvc handler interceptor

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

api ×1

asp.net-mvc ×1

c# ×1

handler ×1

interceptor ×1