When the pageBaseType attribute is set to an assembly-qualified type name - the razor parser appears not to be able to identify a generic base type when the @model keyword is used.
To test - add these classes to an empty MVC 4 site:
public abstract class CustomPage : WebViewPage
{
public CustomPage() { Message = "NonGeneric"; }
public string Message { get; set; }
}
public abstract class CustomPage<TModel> : WebViewPage<TModel>
{
public CustomPage() { Message = "Generic"; }
public string Message { get; set; }
}
Compile the project, and then open the ~/Views/web.config - change the pageBaseType attribute to (note - [assembly_name] and [root_namespace] must be changed accordingly):
<pages pageBaseType="[root_namespace].CustomPage, [assembly_name], Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
Now save.
Add a new action to an existing controller so we can display a view. Let's call it 'TestPageBaseType' - have it simply return View().
Add a new razor view with the same name - initially with this markup:
@{
ViewBag.Title = "Test";
}
<h2>Test</h2>
<p>@Message
</p>
Launch the /[controller]/TestPageBaseType url in a browser - you should see 'NonGeneric' in the html.
Now add this line to the start of the view:
@model string
Compile and run again. You expect to see 'Generic' in the html (because the CustomPage<string> type should be used) - but instead you still see 'NonGeneric'.
As a workaround you can use only the type name in the attribute and it appears to work fine.
Assembly qualified type names should work - they do in Razor v1.
Comments: We considered, and since this is the only report for this issue as well as a good workaround we have decided not to fix.
To test - add these classes to an empty MVC 4 site:
public abstract class CustomPage : WebViewPage
{
public CustomPage() { Message = "NonGeneric"; }
public string Message { get; set; }
}
public abstract class CustomPage<TModel> : WebViewPage<TModel>
{
public CustomPage() { Message = "Generic"; }
public string Message { get; set; }
}
Compile the project, and then open the ~/Views/web.config - change the pageBaseType attribute to (note - [assembly_name] and [root_namespace] must be changed accordingly):
<pages pageBaseType="[root_namespace].CustomPage, [assembly_name], Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
Now save.
Add a new action to an existing controller so we can display a view. Let's call it 'TestPageBaseType' - have it simply return View().
Add a new razor view with the same name - initially with this markup:
@{
ViewBag.Title = "Test";
}
<h2>Test</h2>
<p>@Message
</p>
Launch the /[controller]/TestPageBaseType url in a browser - you should see 'NonGeneric' in the html.
Now add this line to the start of the view:
@model string
Compile and run again. You expect to see 'Generic' in the html (because the CustomPage<string> type should be used) - but instead you still see 'NonGeneric'.
As a workaround you can use only the type name in the attribute and it appears to work fine.
Assembly qualified type names should work - they do in Razor v1.
Comments: We considered, and since this is the only report for this issue as well as a good workaround we have decided not to fix.