【问题标题】:How to call one controller from another controller with Header Request in C#?如何在 C# 中使用 Header Request 从另一个控制器调用一个控制器?
【发布时间】:2018-12-16 17:03:50
【问题描述】:

如何在 C# 中使用 Header Request 从另一个控制器调用一个控制器?有人可以告诉我。这是 ctrlABC 的代码,我想用标头请求调用 ctrlABCD 方法,因为 ctrlABCD 方法也可以直接通过 URL 工作,所以我不想编辑它

public string GetDetailAPI(ModelABC obj)
        {
            var response = "";
            string ControllerName = "CtrlABCD/GetDetail/";
                string BaseAddress = "http://192.168.3.21/WFXWebAPI/";
                string pageParams = "ID|" + obj.QueueTaskKey + "~";
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Add("pageParams", pageParams);
                    client.BaseAddress = new Uri(BaseAddress);
                    response = client.GetStringAsync("api/" + ControllerName).Result.ToString();
                }
            }
            return response;
        }

这是ctrlABCD的代码

public DataSet GetDetail()
        {
            obj = new ClsABCD();
            string pageParams="";
            var re = Request;
            var headers = re.Headers;
            if (headers.Contains("pageParams"))
                pageParams = headers.GetValues("pageParams").FirstOrDefault();
            return obj.GetDetail(pageParams);
        }

【问题讨论】:

  • 用 HttpWebRequest 调用它,就像你从消费者那里做的那样
  • @mjwills 是的。它们是,代码从 CtrlABC.cs 运行,我想用标题调用 CtrlABCD.cs 的方法。

标签: c# asp.net asp.net-mvc api asp.net-web-api


【解决方案1】:

可以使用重载方法RedirectToAction(string actionName, string controllerName);

return RedirectToAction("Index", "MyController");

@Html.ActionLink("Link Name","Index", "MyController", null, null)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 2016-04-30
    • 1970-01-01
    相关资源
    最近更新 更多