【发布时间】:2015-04-02 08:48:19
【问题描述】:
我对 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() 方法。请问怎么解决这个问题
【问题讨论】:
-
然后将传递给服务的 Textbox 和 id 的值是什么?
-
我希望用户在文本框中输入一个字符串,该字符串将作为id调用服务方法GetRuleDetail
标签: c# asp.net wcf rest asp.net-mvc-4