asp.net mvc 4中应出现错误CS1513:}
本文关键字:错误 CS1513 net mvc asp | 更新日期: 2023-09-27 18:20:28
我有一个非常简单的视图。它包含一些C#代码。
@{
ViewBag.Title = "Community";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="req1">
<form>
<input id="txt1" type="text" name="txt1" />
</form>
</div>
<div id="btn1">Send</div>
<div id="res1"></div>
@{
public string GetPassage(string strSearch)
{
using (var c = new System.Net.WebClient())
{
string url = "http://www.esvapi.org/v2/rest/passageQuery?key=IP&passage=' + strSearch + '&options=include-passage-references=true";
return c.DownloadString(Server.UrlDecode(url));
}
}
}
我不知道怎么了。错误消息为:
Source Error:
Line 117:EndContext("~/Views/Home/Community.cshtml", 236, 9, true);
更新:
如果我把代码移到控制器上。
public ActionResult Community()
{
ViewBag.Message = "";
return View();
}
public string GetPassage(string strSearch)
{
using (var c = new System.Net.WebClient())
{
string url = "http://www.esvapi.org/v2/rest/passageQuery?key=IP&passage=" + strSearch + "&options=include-passage-references=true";
return c.DownloadString(Server.UrlDecode(url));
}
}
我想在这个例子的基础上进行一个ajax调用。javascript中的代码怎么样?
视图不是声明方法的合适位置。事实上,在视图中@{
和}
之间编写的所有代码都在同一个方法中运行(不完全正确,但说明了这一点)。显然,在C#中,在另一个方法中声明一个方法是不可能的,视图引擎只是没有足够的方法来将其字面翻译给您。
但是,如果您需要在视图上使用一些实用方法,您可以创建一个委托并稍后调用它:
@{
ViewBag.Title = "Community";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="req1">
<form>
<input id="txt1" type="text" name="txt1" />
</form>
</div>
<div id="btn1">Send</div>
<div id="res1"></div>
@{
Func<string, string> getPassge = strSearch =>
{
using (var c = new System.Net.WebClient())
{
string url = "http://www.esvapi.org/v2/rest/passageQuery?key=IP&passage=' + strSearch + '&options=include-passage-references=true";
return c.DownloadString(Server.UrlDecode(url));
}
};
}