如何使用C#中的Header Request从另一个控制器调用一个控制器?

时间:2018-07-09 04:41:20

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

如何使用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);
        }

1 个答案:

答案 0 :(得分:1)

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

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

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