将cshtml转换为vbhtml

时间:2016-03-10 19:33:15

标签: razor

我正在将使用C#创建的部分视图转换为VB,并需要一些帮助来转换以下源代码段,



@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    {
    @Html.AntiForgeryToken()

    <ul class="nav navbar-top-links navbar-right">
        <li>
            @Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })
        </li>
        <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
    </ul>
    }
}
else
{
    <ul class="nav navbar-top-links navbar-right">
        <li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
        <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
    </ul>
}
&#13;
&#13;
&#13;

我收到以下Parse错误消息  &#34;类&#34;是保留字,不能在隐式表达式中使用。必须使用显式表达式(&#34; @()&#34;)。

1 个答案:

答案 0 :(得分:1)

引用docs.microsoft.com

  

在服务器代码块中,您经常需要向页面输出文本和标记。如果服务器代码块包含的文本不是代码而应该按原样呈现,那么ASP.NET需要能够将该文本与代码区分开来。例如:

@If IsPost Then
    ' This line has all content between matched <p> tags.
    @<p>Hello, the time is @DateTime.Now and this page is a postback!</p> 
Else
    ' All content between matched tags, followed by server code.
    @<p>Hello, <em>Stranger!</em> today is: </p> @DateTime.Now
End If

此处您的代码已转换:

@Imports Microsoft.AspNet.Identity
@If Request.IsAuthenticated Then
    Using Html.BeginForm("LogOff", "Account", FormMethod.Post, New With {.id = "logoutForm", .class = "navbar-right"})
        @Html.AntiForgeryToken()

        @<ul class="nav navbar-top-links navbar-right">
            <li>@Html.ActionLink("Hello " & User.Identity.GetUserName() & "!", "Manage", "Account", routeValues:=Nothing, htmlAttributes:=New With {Key .title = "Manage"})</li>
            <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
        </ul>
    End Using
Else
    @<ul class="nav navbar-top-links navbar-right">
        <li>@Html.ActionLink("Register", "Register", "Account", routeValues:=Nothing, htmlAttributes:=New With {Key .id = "registerLink"})</li>
        <li>@Html.ActionLink("Log in", "Login", "Account", routeValues:=Nothing, htmlAttributes:=New With {Key .id = "loginLink"})</li>
    </ul>
End If