首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误CS1513:}预期在asp.net mvc 4中

错误CS1513:}预期在asp.net mvc 4中
EN

Stack Overflow用户
提问于 2013-06-29 20:49:35
回答 1查看 13.4K关注 0票数 0

我有一个非常简单的观点。它包含一些C#代码。

代码语言:javascript
复制
@{
   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));               
       }
    }
 }

我不知道是怎么回事。错误信息是:

代码语言:javascript
复制
Source Error:

Line 117:EndContext("~/Views/Home/Community.cshtml", 236, 9, true);

更新:

如果我把代码移到控制器上。

代码语言:javascript
复制
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中的代码如何?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-29 21:04:10

视图不是声明方法的合适位置。实际上,在@{}之间的视图中编写的所有代码都是在同一个方法中运行的(不完全正确,但要说明要点)。显然,在C#中不可能在另一个方法中声明一个方法,视图引擎只是没有足够的方法来将它逐字逐句地翻译过来。

但是,如果您需要在视图上使用一些实用程序方法--您可以创建一个委托,然后调用它:

代码语言:javascript
复制
@{
   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));
        }
    };
 }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17384543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档