CS1003: Syntax error, '>' expected in Razo

2020-06-01 08:01发布

问题:

I'm trying something new (to me) in using an abstract base class for my layout viewmodel.

The problem is that when I run the site as is, it throws a very cryptic (to me) exception. What does this exception mean, and what might I do to resolve it?

Layout

@model MyApp.Core.ViewModels.LayoutViewModel

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@Model.Title</title>
</head>
<body>
    <div>
       @RenderBody()
    </div>
</body>
</html>

Index

@model MyApp.Core.ViewModels.Home.IndexViewModel;

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<h1>@Model.Body</h1>

LayoutViewModel

namespace MyApp.Core.ViewModels
{
    public abstract class LayoutViewModel
    {
        public string Title { get; set; }
    }
}

IndexViewModel

namespace MyApp.Core.ViewModels.Home
{
    public class IndexViewModel : LayoutViewModel
    {
        public string Body { get; set; }
    }
}

Controller

[HttpGet]
public ActionResult Index()
{
    var model = new IndexViewModel
        {
            Title = "Hello World",
            Body = "Hello World"
        };


    return View(model);
}

And the Exception

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 27:
Line 28:
Line 29:     public class _Page_Views_Home_Index_cshtml : 
System.Web.Mvc.WebViewPage<FutureStateMobile.Core.ViewModels.Home.IndexViewModel;>
{ 
Line 30:          
Line 31: #line hidden

Source File: c:\Users\Chase\AppData\Local\Temp\Temporary ASP.NET Files\root\b314e0d7\36f522db\App_Web_index.cshtml.a8d08dba.yr7oemfz.0.cs Line: 29

回答1:

Compare and contrast:

Layout

@model MyApp.Core.ViewModels.LayoutViewModel

Index

@model MyApp.Core.ViewModels.Home.IndexViewModel;

Got it yet? Here's the answer:

one of them has a ; which shouldn't be there



回答2:

I just add the line twice, and deleted... problem solved!

@model MyApp.Core.ViewModels.LayoutViewModel
@model MyApp.Core.ViewModels.Layout_OrOther_Model

Try to compile, you get an error (only one model blah, blah..) Delete one of them.

@model MyApp.Core.ViewModels.LayoutViewModel

Compile.

That works for me!



回答3:

bad:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<DSNY.Core.Interfaces.IUser>" %>

versus

good:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<DSNY.Core.Interfaces.IUser>>" %>

Compiler kept telling me it was expecting an extra >.