在 asp.net mvc 4 应用程序中使用 Restful WCF 服务
Consume Restful WCF service in asp.net mvc 4 application
我是 asp.net mvc 的新手。我在 asp.net mvc4 应用程序中使用 rest web 服务时遇到问题。
这是服务的界面:
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "GetRuleDetail/{id}")]
string GetRuleDetail(string id);
}
在我的 mvc 应用程序中,我添加了我的服务作为服务参考 "ServiceReference1"
然后我创建了一个控制器:
public ActionResult Index()
{
string strjson = Request["Json"].ToString();
//string strjson = "input={\"name\": \"obj1\",\"x\": 11,\"y\":20,\"obj\":{\"testKey\":\"val\",},\"tab\":[1 , 2, 46]}";
ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
return View(obj.GetRuleDetail(strjson));
}
字符串strjson,我想从具有以下代码的视图中传递它:
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";} <h2>Index</h2> <section class="contact">
<header>
<h3>Enter your JSON string</h3>
</header>
<p>
<ol>
<li>
@Html.Label("Json")
@Html.TextBox("txtJson")
</li>
</ol>
</p>
<p>
<button>Test</button>
</p>
我错过了什么吗? Cz strjson 始终为空,并且在我在文本框中输入我的 jsonstring 之前执行 Index() 方法。我该如何解决这个问题
这是不正确的方法,首先您需要渲染一个视图,然后 post 将它与 id 一起发送到服务器,然后将传递给服务器 service.You 需要创建一个模型,它将是绑定查看。
第一个渲染视图
public ActionResult Index()
{
return View();//Tihs will simply return view
}
这是绑定视图的模型class
public class JsonData
{
public string Id { get; set; }
}
这将是您的观点
@model JsonData
@using (Html.BeginForm("GetServiceData", "ControllerName", FormMethod.Post))
{
@Html.Label("Json")
@Html.TextBoxFor(m=>m.Id)
<input type="submit" value="Submit" />
}
现在,当您post这个视图时,它将与文本框中的数据一起进入控制器
[HttpPost]
public ActionResult GetServiceData(JsonData model)
{
ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
return View(obj.GetRuleDetail(model.Id));//Tihs will simply return view
}
我是 asp.net mvc 的新手。我在 asp.net mvc4 应用程序中使用 rest web 服务时遇到问题。
这是服务的界面:
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "GetRuleDetail/{id}")]
string GetRuleDetail(string id);
}
在我的 mvc 应用程序中,我添加了我的服务作为服务参考 "ServiceReference1"
然后我创建了一个控制器:
public ActionResult Index()
{
string strjson = Request["Json"].ToString();
//string strjson = "input={\"name\": \"obj1\",\"x\": 11,\"y\":20,\"obj\":{\"testKey\":\"val\",},\"tab\":[1 , 2, 46]}";
ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
return View(obj.GetRuleDetail(strjson));
}
字符串strjson,我想从具有以下代码的视图中传递它:
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";} <h2>Index</h2> <section class="contact">
<header>
<h3>Enter your JSON string</h3>
</header>
<p>
<ol>
<li>
@Html.Label("Json")
@Html.TextBox("txtJson")
</li>
</ol>
</p>
<p>
<button>Test</button>
</p>
我错过了什么吗? Cz strjson 始终为空,并且在我在文本框中输入我的 jsonstring 之前执行 Index() 方法。我该如何解决这个问题
这是不正确的方法,首先您需要渲染一个视图,然后 post 将它与 id 一起发送到服务器,然后将传递给服务器 service.You 需要创建一个模型,它将是绑定查看。
第一个渲染视图
public ActionResult Index()
{
return View();//Tihs will simply return view
}
这是绑定视图的模型class
public class JsonData
{
public string Id { get; set; }
}
这将是您的观点
@model JsonData
@using (Html.BeginForm("GetServiceData", "ControllerName", FormMethod.Post))
{
@Html.Label("Json")
@Html.TextBoxFor(m=>m.Id)
<input type="submit" value="Submit" />
}
现在,当您post这个视图时,它将与文本框中的数据一起进入控制器
[HttpPost]
public ActionResult GetServiceData(JsonData model)
{
ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
return View(obj.GetRuleDetail(model.Id));//Tihs will simply return view
}