Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

"page" variable in Razor breaks in 2.0? #6931

Closed
Daniel15 opened this issue Oct 10, 2017 · 7 comments
Closed

"page" variable in Razor breaks in 2.0? #6931

Daniel15 opened this issue Oct 10, 2017 · 7 comments

Comments

@Daniel15
Copy link
Contributor

Daniel15 commented Oct 10, 2017

I have a _Pagination.cshtml view that uses a page variable, like this:

@model Daniel15.Web.ViewModels.Shared.PaginationModel
...
	@for (var page = 1; page <= Model.TotalPages; page++)
	{
		if (page == Model.CurrentPage)
		{
			<strong>@page</strong>
		}
		else
		{
			<a href="@Model.UrlGenerator(page)">@page</a>
		}	
	}

After upgrading from 1.1 to 2.0, I'm hitting this error:

The 'page` directive must appear at the start of the line.
+
			<strong>@page</strong>
The 'page' directives value(s) must be separated by whitespace.
+
			<strong>@page</strong>
The 'page` directive must appear at the start of the line.
+
			<a href="@Model.UrlGenerator(page)">@page</a>
The 'page' directive may only occur once per document.
+
			<a href="@Model.UrlGenerator(page)">@page</a>
The 'page' directives value(s) must be separated by whitespace.
+
			<a href="@Model.UrlGenerator(page)">@page</a>

I guess this is due to Razor pages (https://docs.microsoft.com/en-us/aspnet/core/mvc/razor-pages/) using the @page directive (and thus disallowing usage of variables called page within Razor), however, shouldn't this be mentioned in the upgrade notes? It's not in the migration docs: https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/

@Eilon
Copy link
Member

Eilon commented Oct 10, 2017

Yup this is indeed a bug in 2.0.0, see #6706

We will have this fixed in the upcoming 2.0 patch. Sorry you ran into this!

@Eilon Eilon closed this as completed Oct 10, 2017
@rynowak
Copy link
Member

rynowak commented Oct 10, 2017

#6706 tracks the use of 'page' as a route parameter when not using razor pages. This is about the usage in Razor code.

Unfortunately any new directive we add becomes a reserved word in Razor.

@Daniel15
Copy link
Contributor Author

Unfortunately any new directive we add becomes a reserved word in Razor.

Which is fine, but please include it in the upgrade documentation. Right now, it's an undocumented breaking change.

@Eilon
Copy link
Member

Eilon commented Oct 10, 2017

@Daniel15 I posted aspnet/Announcements#272 in our list of breaking changes. Sorry for missing this! (These announcement issues are linked to from the migration doc at https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/)

@John0King
Copy link

@Eilon @Daniel15 why not just use @razorpage or @razor-page as the directive for razor pages, nobody use it as a variable .

@Eilon
Copy link
Member

Eilon commented Oct 11, 2017

@John0King well, for now it's too late to change it because it already shipped. But that's something good for us to keep in mind when adding other new directives or reserved words.

@John0King
Copy link

@Eilon My concern is that page is probably a very common variable in web development, at least 60% website use the it as a query parameter for paging , and will create a page variable to display and dealing with it.

and other side, change current razor pages to use new directive is easy, search every .cshtml file and replace ^@page\s to the new directive

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants