【问题标题】:Syntax error in temporary build file临时构建文件中的语法错误
【发布时间】:2016-02-11 03:50:11
【问题描述】:

我正在为内部网站构建一个相当标准的 razor/c# 页面。它基本上是一个为 db 查询提供有效参数的查询的包装器。

页面在构建时在 Asp_Web_?????.cshtml 中生成错误。对于我的一生,我找不到错误。以下是页面的来源

@{
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "ABC Floor Limits";
}

<hgroup class="title">
    <h1>@Page.Title.</h1>
    <h2>Adjust.</h2>
</hgroup>


@{
    var DbCustom = Database.Open("Online_Ctrack6_Custom");
    var DbCustLogs = Database.Open("Online_CustLogs");

    int vehicleId = -1;
    string currentAction;

    if(IsPost)
    {
        vehicleId = Request.Form["vehicleId"].AsInt();
        string applianceName = Request.Form["applianceName"];
        int floorHours = Request.Form["hours"].AsInt();
        DateTime? dateToParse;
        DateTime dateTaken;
        string result = "";


        if (string.IsNullOrEmpty(Request.Form["dateTaken"]) == false)
        {
            var dtValue = new DateTime();
            if (DateTime.TryParse(Request.Form["dateTaken"], out dtValue))
            {
                dateToParse = dtValue;
            }
            else
            {
                dateToParse = null;
            }
        }
        else
        {
            dateToParse = null;
        }

        currentAction = Request.Form["action"];
        if (currentAction == "updateHours")
        {
            try
            {
                dateTaken = (DateTime)dateToParse;
                result = doProcessHoursForm(DbCustLogs, vehicleId, applianceName, dateTaken, floorHours);
            }
            catch (InvalidDataException ex)
            {
                @:<div class="error">@ex.Message</div>
            }
            catch (ArgumentNullException ex)
            {
                @:<div class="error">@ex.ParamName cannot be null.<br />@ex.Message @ex.ParamName</div>
            }
            finally
            {
                @:<div class="result">@result</div>
            }

        }
    }

    List<SelectListItem> listVehicles = null;
    try 
    {
        listVehicles = doCreateVehicleList(DbCustom, vehicleId);
    }
    catch (Exception ex)
    {
        @:<div class="error">@ex.Message<br />@ex.InnerException.Message</div>
    }

    string floorSql = "SELECT [NodeId],[PrimaryPropertyValue],[ID],[PumpADurationSec],[PumpAFloorSec],[PumpAFloorDate],[PumpATotalSec],[PumpBDurationSec],[PumpBFloorSec],[PumpBFloorDate],[PumpBTotalSec],[VacuumDurationSec],[VacuumFloorSec],[VacuumFloorDate],[VacuumTotalSec]   FROM [Ctrack6_Custom].[dbo].[vwVeoliaRunningTotals] ORDER BY [Id]";
    var floorGrid = new WebGrid(source: DbCustom.Query(floorSql), canPage: false, ajaxUpdateContainerId: "floorGrid");
}

<div class="section">
    <h3>Current Floor Values</h3>
    <p>This table lists the current floor values for ABC .  The values are saved internally as a number of seconds.</p>

@floorGrid.GetHtml(
    tableStyle: "VeoliaFloorTable",
    headerStyle: "columnHead",
    alternatingRowStyle: "altRow",
    columns: floorGrid.Columns(
        floorGrid.Column(columnName: "NodeId", header: @Functions.Sorter("NodeId", "Node Id", floorGrid)),
        floorGrid.Column(columnName: "Id", header: @Functions.Sorter("Id", "Vehicle", floorGrid)),

        floorGrid.Column(columnName: "PumpAFloorSec", header: @Functions.Sorter("PumpAFloorSec", "Pump A Floor Sec", floorGrid), style: "alignRight"),
        floorGrid.Column(columnName: "PumpAFloorDate", header: @Functions.Sorter("PumpAFloorDate", "Pump A Floor Date", floorGrid), style: "nowrap"),

        floorGrid.Column(columnName: "PumpBFloorSec", header: @Functions.Sorter("PumpBFloorSec", "Pump B Floor Sec", floorGrid), style: "alignRight"),
        floorGrid.Column(columnName: "PumpBFloorDate", header: @Functions.Sorter("PumpBFloorDate", "Pump B Floor Date", floorGrid), style: "nowrap"),


        floorGrid.Column(columnName: "VacuumFloorSec", header: @Functions.Sorter("VacuumFloorSec", "Vacuum Floor Sec", floorGrid), style: "alignRight"),
        floorGrid.Column(columnName: "VacuumFloorDate", header: @Functions.Sorter("VacuumFloorDate", "Vacuum Floor Date", floorGrid), style: "nowrap")

    )
);

</div>

<div class="section">
    <h3>Update Floor Limits</h3>
    <p>This form allows you to update the floor limit specified for an appliance mounted on a vehicle.</p>
    <form method="post">
        <input type="hidden" name="username" value="@WebSecurity.CurrentUserName" />
        <input type="hidden" name="action" value="updateHours" />
        <label for="selectVehicleControl">Choose Geozone:</label>
        @Html.DropDownList("vehicleId", listVehicles)

        <label for="selectApplianceControl">Choose Appliance:</label>
        <select name="applianceName" id="selectApplianceControl">
            <option value="-1">Choose an Appliance</option>
            <option value="Pump A">Pump A</option>
            <option value="Pump B">Pump B</option>
            <option value="Vacuum">Vacuum</option>
        </select>

        <label for="inputHoursText">Hour Reading:</label>
        <input name="hours" type="number" id="inputHoursText" min="0" max="9999999" size="7" required="required" />
        <span style="font-size:small">This will be converted into seconds</span>
        <br />

        <label for="dateTakenControl">Date Taken:</label>
        <input name="dateTaken" class="datetimefield" type="datetime" id="dateTakenControl" maxlength="19" required="required" />
        <br />

        <input type="submit" value="Update Hour Reading" />
    </form>
</div>

@{

    public string doProcessHoursForm(Database DbCustLogs, int vehicleId, string applianceName, DateTime dateTaken, int floorHours)
    {
        int floorSeconds = floorHours * 3600;
        string sqlHoursUpdate = "exec sp_Veolia_update_floor_limit @0, @1, @2, @3";

        if(vehicleId != null && applianceName != null && dateTaken != null && floorSeconds != null)
        {
            var result = DbCustLogs.Execute(sqlHoursUpdate, vehicleId, applianceName, dateTaken.ToString("yyyy-MM-dd HH:mm:ss"), floorSeconds);
            if ( result != 1)
            {
                throw new InvalidDataException("Operation should only affect one record");
            }
            else 
            {
                return result + " row(s) changed";
            }
        }
        else
        {
            string nullArg = "";
            if (vehicleId == null) { nullArg += "vehicleId,";}
            if (applianceName == null) { nullArg += "applianceName,"; }
            if (dateTaken == null) { nullArg += "dateTaken,"; }
            if (floorSeconds == null) { nullArg += "floorSeconds,"; }

            if (nullArg.Length > 1) { nullArg = nullArg.TrimEnd(','); }

            throw new ArgumentNullException(nullArg, "Argument cannot be null");
        }
        return "An error ocured";
    }

    public List<SelectListItem> doCreateVehicleList(Database db, int vehicleId)
    {
        var sqlVehicles = "SELECT NodeId, Id as VehicleName FROM vwVeoliaNodes ORDER BY Id";

        List<SelectListItem> listTemp = new List<SelectListItem>();
        listTemp.Add(new SelectListItem
        {
            Text = "Select a Vehicle",
            Value = "-1",
            Selected = false
        });

        try 
        {
            foreach (var item in db.Query(sqlVehicles))
            {
                if (item.NodeId == vehicleId)
                {
                    listTemp.Add(new SelectListItem
                    {
                        Text = item.VehicleName,
                        Value = item.NodeId.ToString(),
                        Selected = true
                    });
                }
                else
                {
                    listTemp.Add(new SelectListItem
                    {
                        Text = item.VehicleName,
                        Value = item.NodeId.ToString(),
                        Selected = false
                    });

                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Exception occurred building Vehicle List", ex);
        }
        return listTemp; 
    }
}

错误详情如下:

错误编号: 1
错误消息: } 预期
文件: c:\Users\Lukem\AppData\ Local\Temp\Temporary ASP.NET Files\root\53d555f7\b262e6b6\App_Web_abcfloor.cshtml.cdcab7d2.abupdzvs.0.cs
行: 760

临时文件中的错误是EndContext引用:

BeginContext("~/VeoliaFloor.cshtml", 5970, 36, true);
WriteLiteral(" />\r\n        </form>\r\n    </div>\r\n\r\n");
EndContext("~/VeoliaFloor.cshtml", 5970, 36, true);

我已安装颜色匹配 {} 工具来提供帮助。页面似乎没有问题,只有编译到临时文件时。这让我发疯了。

【问题讨论】:

    标签: c# asp.net razor syntax


    【解决方案1】:

    这是因为您在声明方法的部分中缺少 @functions 关键字。

    【讨论】:

    • 如果我不使用 MVC,只使用剃须刀?
    • 你会怎么做呢?你的项目不是 MVC 的吗?
    • 这是一个“网页”项目,所以没有 MVC,但有 Razor。 bin 中对以下内容的引用: - Microsoft.Web.Helpers.dll - Microsoft.Web.Infrastructure.dll - Microsoft.Web.WebPages.OAuth.dll - System.Web.Razor.dll - System.Web.WebPages.Deployment.dll - System.Web.WebPages.dll - System.Web.WebPages.Razor.dll
    • 哦,你的意思是一个网站项目。看来您在声明方法的部分中缺少 @functions 关键字。添加后我不再收到该错误,但现在问题出在@Functions.Sorter。这是在哪里定义的?顺便说一句,您正在将一些 int 变量与 NULL 进行比较,这些变量不可为空,因此您会收到警告。
    • Functions.cshtml 在 App_Code 下定义。此时它只是持有一个排序的方法
    猜你喜欢
    • 2019-11-14
    • 2015-10-22
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    • 2013-03-30
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多