MVC控制器无法触发视图

时间:2016-06-20 05:53:37

标签: c# asp.net-mvc multithreading model-view-controller

嗨,视图无法启动,但它已经击中了它的控制器。 这是一个僵局吗?因为我使用GetAsync来获取HttpResponse消息。 这是我的代码。

   private bool GetCoordinates(string address)
    {
        bool result = false;

        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri(HttpUtility.UrlPathEncode("http://locationInfo/GetLocation?address=" + address));

        client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage response = client.GetAsync("").Result;
        if (response.IsSuccessStatusCode)
        {
            JObject objs = JObject.Parse(response.Content.ReadAsStringAsync().Result);
            int count = 0;
            string[] cor = new string[2];
            foreach (var item in objs)
            {
                if(item.Key.ToString() == "location") {
                    foreach (var it in item.Value)
                    {
                        if (count <= 1)
                        {
                            cor[count] = it.ToList()[0].ToString();
                        }
                        count++;
                    }
                }
            }

            Xcor = cor[0];
            Ycor = cor[1];
            result = response.IsSuccessStatusCode;
        }

        response.Dispose();
        client.Dispose();
        return result;
    }

}

- &GT;索引无法显示该视图。

2 个答案:

答案 0 :(得分:0)

如果希望控制器在所有进程之后返回布尔值,则将此方法设置为操作方法(此处我们可以返回要查看的值)

[HttpPost]
public ActionResult Index(string address)
{       
    ModelClass model = new ModelClass();
    model.yourModelVal = GetCoordinates(address);
    return View(model);
}

public bool GetCoordinates(string address)
{
    bool result = false;

    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(HttpUtility.UrlPathEncode("http://locationInfo/GetLocation?address=" + address));

    client.DefaultRequestHeaders.Accept.Add(
    new MediaTypeWithQualityHeaderValue("application/json"));

    HttpResponseMessage response = client.GetAsync("").Result;
    if (response.IsSuccessStatusCode)
    {
        JObject objs = JObject.Parse(response.Content.ReadAsStringAsync().Result);
        int count = 0;
        string[] cor = new string[2];
        foreach (var item in objs)
        {
            if(item.Key.ToString() == "location") {
                foreach (var it in item.Value)
                {
                    if (count <= 1)
                    {
                        cor[count] = it.ToList()[0].ToString();
                    }
                    count++;
                }
            }
        }

        Xcor = cor[0];
        Ycor = cor[1];
        result = response.IsSuccessStatusCode;
    }

    response.Dispose();
    client.Dispose();
    return result;
}

答案 1 :(得分:0)

您需要返回View。现在您要返回boolReturnType必须为ActionResult