将用户定向到 ASP.Net MVC 应用程序中的另一个页面会导致自动生成的代码出现语法错误

Directing user to another page in ASP.Net MVC application leads to syntax error in auto-generated code

我的 _layout.cshtml 文件中的片段

<body>
<!--
NAVBAR
-->
<div class="navBar">
    <input id="homeButton" type="submit" name="homeButton" value="Home"/>
    <input id="toolsButton" type="submit" name="toolsButton" value="Tools"/>
    <input id="contactButton" type="submit" name="contactButton" value="Contact Me"/>
    <input id="supportButton" type="submit" name="supportButton" value="Support"/>
    <input id="aboutButton" type="submit" name="aboutButton" value="About"/>
</div><!--TODO: STYLE THESE -->

<script>
const homeButton = document.getElementById("homeButton");
const toolsButton = document.getElementById("toolsButton");
const contactButton = document.getElementById("contactButton");
const supportButton = document.getElementById("supportButton");
const aboutButton = document.getElementById("aboutButton");

homeButton.addEventListener("click", () => {
    window.location.href = "/";
});

toolsButton.addEventListener("click", () => {
    window.location.href = "/Tools/Index"; // "/Tools" doesn't work either
});

contactButton.addEventListener("click", () => {
    window.location.href = "/";
});

supportButton.addEventListener("click", () => {
    window.location.href = "/";
});

aboutButton.addEventListener("click", () => {
    window.location.href = "/";
});
</script>

点击按钮时产生的错误

Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1003: Syntax error, '>' expected

Source Error:


Line 29:     
Line 30:     
Line 31:     public class _Page_Views_Tools_Index_cshtml : System.Web.Mvc.WebViewPage<LgbtqWebsiteNoDb.Models.Tool;> {
Line 32:         
Line 33: #line hidden


Source File: c:\Users\marfx\AppData\Local\Temp\Temporary ASP.NET Files\root\ec7221e8a6fd7fe\App_Web_index.cshtml.f024d85f.citsmrxd.0.cs    Line: 31 

详细的编译器输出

C:\Program Files (x86)\IIS Express> "C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe" /t:library /utf8output /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Web\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll" /R:"C:\Users\marfx\AppData\Local\Temp\Temporary ASP.NET Files\root\ec7221e8a6fd7fe\assembly\dl3f9e4d5[=13=]0e9c93_3d27cf01\System.Web.Optimization.dll" /R:"C:\Users\marfx\AppData\Local\Temp\Temporary ASP.NET Files\root\ec7221e8a6fd7fe\App_global.asax.ewmqttqt.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.Extensions\v4.0_4.0.0.0__31bf3856ad364e35\System.Web.Extensions.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel.Activities\v4.0_4.0.0.0__31bf3856ad364e35\System.ServiceModel.Activities.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.Services\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.Services.dll" /R:"C:\Users\marfx\AppData\Local\Temp\Temporary ASP.NET Files\root\ec7221e8a6fd7fe\assembly\dl3\b5be25e0[=13=]3c87e2_1a87d401\System.Web.WebPages.Razor.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.ApplicationServices\v4.0_4.0.0.0__31bf3856ad364e35\System.Web.ApplicationServices.dll" /R:"C:\Users\marfx\AppData\Local\Temp\Temporary ASP.NET Files\root\ec7221e8a6fd7fe\assembly\dl3f8a528[=13=]1bc110_4318cf01\WebGrease.dll" /R:"C:\Users\marfx\AppData\Local\Temp\Temporary ASP.NET Files\root\ec7221e8a6fd7fe\assembly\dl3a6f1e22[=13=]16534c_1a87d401\System.Web.Razor.dll" /R:"C:\Users\marfx\AppData\Local\Temp\Temporary ASP.NET Files\root\ec7221e8a6fd7fe\assembly\dl3[=13=]bd5514f82d14f_c649d701\LgbtqWebsiteNoDb.dll" /R:"C:\Users\marfx\AppData\Local\Temp\Temporary ASP.NET Files\root\ec7221e8a6fd7fe\assembly\dl3eada7ec[=13=]3c87e2_1a87d401\System.Web.WebPages.dll" /R:"C:\Users\marfx\AppData\Local\Temp\Temporary ASP.NET Files\root\ec7221e8a6fd7fe\assembly\dl3\bfe09c51[=13=]3c87e2_1a87d401\System.Web.Helpers.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.Serialization\v4.0_4.0.0.0__b77a5c561934e089\System.Runtime.Serialization.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.VisualStudio.Web.PageInspector.Loader\v4.0_1.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Web.PageInspector.Loader.dll" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel\v4.0_4.0.0.0__b77a5c561934e089\System.ServiceModel.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel.Activation\v4.0_4.0.0.0__31bf3856ad364e35\System.ServiceModel.Activation.dll" /R:"C:\Users\marfx\AppData\Local\Temp\Temporary ASP.NET Files\root\ec7221e8a6fd7fe\assembly\dl303d10e[=13=]c8d184_3aaece01\Antlr3.Runtime.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel.Web\v4.0_4.0.0.0__31bf3856ad364e35\System.ServiceModel.Web.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll" /R:"C:\Users\marfx\AppData\Local\Temp\Temporary ASP.NET Files\root\ec7221e8a6fd7fe\assembly\dl3611dac[=13=]1cbe16_536acd01\Microsoft.Web.Infrastructure.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.ComponentModel.DataAnnotations\v4.0_4.0.0.0__31bf3856ad364e35\System.ComponentModel.DataAnnotations.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.WorkflowServices\v4.0_4.0.0.0__31bf3856ad364e35\System.WorkflowServices.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.IdentityModel\v4.0_4.0.0.0__b77a5c561934e089\System.IdentityModel.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Activities\v4.0_4.0.0.0__31bf3856ad364e35\System.Activities.dll" /R:"C:\Users\marfx\AppData\Local\Temp\Temporary ASP.NET Files\root\ec7221e8a6fd7fe\assembly\dl39223b9[=13=]3c87e2_1a87d401\System.Web.WebPages.Deployment.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll" /R:"C:\Users\marfx\AppData\Local\Temp\Temporary ASP.NET Files\root\ec7221e8a6fd7fe\assembly\dl3\b4261359[=13=]cdd33c_1a87d401\System.Web.Mvc.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.DynamicData\v4.0_4.0.0.0__31bf3856ad364e35\System.Web.DynamicData.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll" /R:"C:\Users\marfx\AppData\Local\Temp\Temporary ASP.NET Files\root\ec7221e8a6fd7fe\assembly\dl3\dd04c433[=13=]eff220_9da8d301\Newtonsoft.Json.dll" /out:"C:\Users\marfx\AppData\Local\Temp\Temporary ASP.NET Files\root\ec7221e8a6fd7fe\App_Web_index.cshtml.f024d85f.citsmrxd.dll" /D:DEBUG /debug+ /optimize- /w:4 /nowarn:1659;1699;1701;612;618 /warnaserror-  "C:\Users\marfx\AppData\Local\Temp\Temporary ASP.NET Files\root\ec7221e8a6fd7fe\App_Web_index.cshtml.f024d85f.citsmrxd.0.cs" "C:\Users\marfx\AppData\Local\Temp\Temporary ASP.NET Files\root\ec7221e8a6fd7fe\App_Web_index.cshtml.f024d85f.citsmrxd.1.cs"


Microsoft (R) Visual C# Compiler version 4.8.4084.0

for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.



This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

c:\Users\marfx\AppData\Local\Temp\Temporary ASP.NET Files\root\ec7221e8a6fd7fe\App_Web_index.cshtml.f024d85f.citsmrxd.0.cs(31,106): error CS1003: Syntax error, '>' expected
c:\Users\marfx\AppData\Local\Temp\Temporary ASP.NET Files\root\ec7221e8a6fd7fe\App_Web_index.cshtml.f024d85f.citsmrxd.0.cs(31,106): error CS1519: Invalid token ';' in class, struct, or interface member declaration

我曾尝试使用 Response.Redirect("/Tools"); 通过 Razor 将用户重定向到其他页面,但这也不起作用,在同一行抛出不同的异常(编译器只是选择了一个不同的字符来抛出) .

我的工具控制器

using System.Collections.Generic;
using System.Web.Mvc;
using LgbtqWebsiteNoDb.Models;
using static LgbtqWebsiteNoDb.Models.Tool;
using Microsoft.Ajax.Utilities;

namespace LgbtqWebsiteNoDb.Controllers
{
    public class ToolsController : Controller
    {
        private static readonly List<Tool> Tools = new List<Tool>()
        {
            new Tool
            {
                ToolName = "Hateful Countries Finder", 
                ReleaseDate = DateTime.Now.Date, 
                UpdateDate = DateTime.Now.Date, //GetLastUpdate(toolId:1), //TODO: Fix this method, it looks for the file in the wrong place
                ToolId = 1,
                ToolDesc = "A tool to find out which countries in the world have anti-Lgbtq+ laws which apply to you", 
                ToolThumbUrl = "", 
                ToolThumbAlt = ""
            },
            new Tool
            {
                ToolName = "Hateful Map", 
                ReleaseDate = DateTime.Now.Date, 
                UpdateDate = DateTime.Now.Date, //GetLastUpdate(toolId:2), 
                ToolId = 2, 
                ToolDesc = "A tool to show how many world countries have generic anti-Lgbtq laws", 
                ToolThumbUrl = "", 
                ToolThumbAlt = ""
            }
        };
        
        public static int ToolCount = Tools.Count;
        
        // GET: Tools/
        public ActionResult Index()
        {
            ViewBag.Title = "Tools | Home";
            
            return View();
        }
        
        // GET: Tools/HatefulCountriesFinder
        public ActionResult HatefulCountriesFinder()
        {
            ViewBag.Title = "Tools | Hateful Countries";
            
            return View();
        }
        
        // GET: Tools/HatefulMap
        public ActionResult HatefulMap()
        {
            ViewBag.Title = "Tools | Hateful Map";
            
            return View();
        }
    }
}

工具模型

public class Tool
    {
        /// <summary>
        /// Getter and Setter for the name of a given tool
        /// </summary>
        public string ToolName { get; set; }

        /// <summary>
        /// Getter and Setter for the date the tool was released
        /// </summary>
        [DataType(DataType.Date)] public DateTime ReleaseDate { get; set; }

        /// <summary>
        /// Getter and Setter for the date the tool was last updated
        /// </summary>
        [DataType(DataType.Date)] public DateTime UpdateDate { get; set; }

        /// <summary>
        /// Getter and Setter for the tool ID
        /// </summary>
        public int ToolId { get; set; }
        
        /// <summary>
        /// Getter and Setter for the description of the tool
        /// </summary>
        public string ToolDesc { get; set; }
        
        /// <summary>
        /// Getter and Setter for the thumbnail of the tool image
        /// </summary>
        public string ToolThumbUrl { get; set; }

        /// <summary>
        /// Getter and Setter for the alt text of the thumbnail
        /// </summary>
        public string ToolThumbAlt { get; set; }
        
        /// <summary>
        /// Getter and Setter for the number of tools; used for enumeration
        /// </summary>
        public int ToolCount { get; set; }

        /// <summary>
        /// A method to update the tool, and change the last updated date
        /// </summary>
        /// <returns>DateTime</returns>
        public static DateTime UpdateTool()
        {
            //TODO: Write the actual code for this
            //TODO: Write new update DateTime to the appropriate file; See GetLastUpdate() for more
            
            return DateTime.Now.Date;
        }
    }
}

Tools/Index.cshtml

@model LgbtqWebsiteNoDb.Models.Tool;

<!DOCTYPE html>
<html lang="en-GB">
<head>
    <title>@ViewBag.Title</title>
    <!-- Removed the bulk of the head -->
</head>

<body>
<div class="title">
        
</div>
<table>
    @for (var i = 0; i < @Model.ToolCount; i++)
    {
        <tr>
            <td>
                <div class="toolTitle">
                    @Model.ToolName;
                </div>
                <div class="toolThumb">
                    <img src=@Model.ToolThumbUrl alt=@Model.ToolThumbAlt>
                </div>
                <div class="toolDesc">
                    @Model.ToolDesc;
                </div>
            </td>
        </tr>
    }
</table>
</body>
</html>

我正在使用 .Net v4.7.2(我会使用 5.0.0,但 none 我的 IDE 似乎想使用最新版本,我时间紧迫所以不能花了很长时间调试所以我只是凑合)

干杯

对我来说,你错过了语法错误,文件 Tools/Index.cshtml

中的 img 标签需要“>”

我不是很确定,但是行尾的 ;:

@model LgbtqWebsiteNoDb.Models.Tool;

Tools/Index.cshtml 中可能搞砸了