Bing Web搜索代码适用于控制台应用程序,但不适用于MVC

时间:2015-01-09 14:40:42

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

我试图在我的MVC项目中使用Bing Search API。奇怪的是,当我通过控制台应用程序运行时,我得到了预期的结果,但是当我尝试在线运行时,当我进入程序并查看变量imageResults的值时,我得不到任何结果。我确实看到了一条消息TotalCount = 'imageResults.TotalCount' threw an exception of type 'System.InvalidOperationException.'我的控制器列在下面。

namespace MusicMan.Controllers
{
    public class BingSearchController : Controller
    {

        // GET: BingSearch
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(string search)
        {
            var bingContainer = new Bing.BingSearchContainer(
                new Uri("https://api.datamarket.azure.com/Bing/Search/"));

            // Replaced the value with my account key.
            var accountKey = "mykey";

            // Configures bingContainer to use your credentials.
            bingContainer.Credentials = new NetworkCredential(accountKey, accountKey);

            // Build the query.
            var imageQuery =
                bingContainer.Image("xbox", null, null, null, null, null, null);

            var imageResults = imageQuery.Execute();

            return View();
        }
    }
}

任何有关它为什么作为控制台应用程序而不是MVC的帮助将不胜感激。

0 个答案:

没有答案