如何从MVC4 C#JSON没有JavaScript和Ajax的无 [英] How to get json from MVC4 C# with no javascript and no Ajax

查看:273
本文介绍了如何从MVC4 C#JSON没有JavaScript和Ajax的无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到我真的应该学习WCF这种(随意评论,如果您同意)的感觉,但是,我想查询一个网站,并得到一个结果返回,XML或JSON格式。

I get the feeling I really should be learning WCF for this (feel free to comment if you agree), but, I want to query a website and get a result back, in either XML or JSON format.

在这种情况下,我选择JSON。

In this case, I'm choosing JSON.

我在一个网站控制器(WWW .site1.com),它看起来像

I have a controller in a web site (www.site1.com) , which looks like

public JsonResult Save(bool willSave)
{
   //logic with the parameters to go here
   return Json(new { code = 200, description = "OK" }, JsonRequestBehavior.AllowGet);
}

现在,我想获得从其他网站这一信息,所以在www.site2.com我什么都没有......我不知道什么样的代码我可以写,只是因为所有我见过的例子,您查询JSON使用JavaScript / AJAX。

Now, I'd like to get this information from another website, so in www.site2.com I have nothing... I have no idea what code I can write, simply because all of the examples I've seen where you query json uses javascript/Ajax.

我不想使用JavaScript或阿贾克斯(我知道如何做到这一点),这个项目我试图尽我所能服务器端。

I don't want to use JavaScript or Ajax (I know how to do that), for this project I'm trying to do everything I can server side.

我希望能够做到以下

public ActionResult Do() 
{
    var json = someHowQuerySite1.com?withQueryString=true;//THIS IS THE ISSUE
    var model = CreateModel(json);
    return View(model);
}



正如你所希望看到的,

As you can hopefully see,

var json = someHowQuerySite1.com?withQueryString=true;//THIS IS THE ISSUE

我不知道在这里写什么语法。

I don't know what syntax to write here.

推荐答案

最简单的方法,替换

var json = someHowQuerySite1.com?withQueryString=true;



with

using (var client = new HttpClient())
{
    var responseString = client.GetStringAsync("http://www.example.com/recepticle.aspx?withQueryString=true");

    var json = myJsonUtililty.toJson(responseString);
}

与帖子

这篇关于如何从MVC4 C#JSON没有JavaScript和Ajax的无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆