ASP.NET MVC strongly typed view compilation error

微笑、不失礼 提交于 2019-12-23 12:58:56

问题


This is a strange one. I changed something (not sure what) and now my app's view doesn't compile at runtime.

The view itself is strongly typed:

<%@ Page Language="C#"
         MasterPageFile="~/Views/Shared/Site.Master"
         Inherits="System.Web.Mvc.ViewPage<MyNamespace.OperatorModel>" %>

When I visit the page, it fails to compile, saying:

CS1061: 'object' does not contain a definition for 'Log' and no extension method 'Log' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

Pretty standard error. The corresponding source code line is:

<%= Html.HiddenFor(model => model.Log) %>

When I look at the compiler-generated code, I see that the base class of the view is not strongly typed:

[System.Runtime.CompilerServices.CompilerGlobalScopeAttribute()]
public class views_operator_create_aspx
    : global::System.Web.Mvc.ViewPage, // NOT STRONGLY TYPED
      System.Web.SessionState.IRequiresSessionState,
      System.Web.IHttpHandler {

So my question is, what is causing the view compiler to ignore my Inherits attribute on the view definition?

I should point out that other views on the same controller are working, and they have exactly the same page declaration as I've shown above.

EDIT Does anyone know where the generated source code file lives, assuming it is persisted somewhere?

EDIT I found the culprit (in my answer below) but I've no idea why this is happening. If someone can explain I'd appreciate it!


回答1:


Are you referencing the OperatorModel properly in your Web.Config? That will allow you to explicitly reference OperatorModel.

<namespaces>
    <add namespace="My.Namespace"/>

alternatively you could change your Inherits value to:

Inherits="System.Web.Mvc.ViewPage<My.Namespace.OperatorModel>"

Also, when passing a model object to the view, make sure you check for null or return an empty OperatorModel:

return View(operatorModel 
    ?? new OperatorModel() { Text = "I can has not found!" });



回答2:


I find when I have totally weird errors like this, sometimes if I delete the shadow copy of all my files it will clear it up. You'll find it at:

%SYSTEMROOT%\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files

Replace whatever version of the Framework you're using in that line above. Delete all the files that correspond to your project (or just delete everything in the directory).

You may have to shut down your w3p service to be sure it lets go of all the files and you can delete it.

Also, I believe you'll find the generated source code you're looking for in there.




回答3:


You didn't happen to delete that special, magical web.config file that lives in the root of your /Views folder, did you?

[This catches me at least once a month]




回答4:


Ok so I pared my view down to this:

<%@ Page Language="C#"
         MasterPageFile="~/Views/Shared/Site.Master"
         Inherits="System.Web.Mvc.ViewPage<MyNamespace.OperatorModel>" %>
<%@ Import Namespace="MyNamespace.Data" %>

<asp:Content ContentPlaceHolderID="MainContent" runat="server">
    <%= GetType() %><br />
    <%= GetType().BaseType %><br />
    <%= GetType().BaseType.BaseType %>
</asp:Content>

The output was, even with all content areas empty:

ASP.views_operator_create_aspx
System.Web.Mvc.ViewPage
System.Web.UI.Page

But if I remove the <%@ Import line, I get this:

ASP.views_operator_create_aspx
System.Web.Mvc.ViewPage`1[MyNamespace.OperatorModel]
System.Web.Mvc.ViewPage

I have no idea why the import should cause the page to no longer be strongly typed. Can someone explain? Any tips on how to debug or trace the compilation would be nice. It's great that I can keep coding now, but this isn't a very satisfying end to the problem.



来源:https://stackoverflow.com/questions/3550244/asp-net-mvc-strongly-typed-view-compilation-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!