MVC3/Razor: cshtml.Execute()': no suitable met

2019-04-04 04:42发布

问题:

I am trying to convert an MVC2 site to MVC3 using RazorViewEngine.

I used this tool to upgrade my project and the Telerik converter tool to convert my .aspx views to Razor. The Telerik tool put an @inherits line at the top of my layouts (inherting from ViewMasterPage).

When I tried to run a page that used one of these layouts, I got the error:

...cshtml.Execute(): no suitable method found to override

I removed the @inherits tag and it started to work for my home page. However, I continued to get this error for another page using the same layout. And now, after moving some things around to deal with an Areas issue, I'm back to getting this error for all my pages (the ones I can get to, anyway).

I have tried closing Visual Studio, deleting temporary files, etc.

回答1:

Figured it out - the following section needs to be in the web.config for razor - I had it in the web.configs in the Views directories, but not in the root web.config:

 <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>


回答2:

There could be a few things going on here. Make sure you are following these guidelines

  • you don't have @inherits directives in your views. unless you are using a custom view page base class they are unnecessary. For strongly-typed views you should use the @model directive to specify the model type. For weekly-typed views you don't need anything.
  • don't mix razor views with aspx master pages (or aspx pages with razor layouts), as they don't work together easily. This includes checking all of your action methods where you have code like return View("ViewName", "MasterName") since that might also cause conflicting templatign technologies to be used.


回答3:

If you still have a backup of your MVC2 project I would try using the tool Microsoft released on MSDN for this. See this link for more information. Also Scott Guthrie wrote something about it in his blog posts when MVC3 was released, you can read the article here.

I'm afraid I can't give you an direct solution to this. But sounds like an bug or problem in the conversion software from telerik you used.