开发者

Trouble getting razor and aspx to play nicely

开发者 https://www.devze.com 2023-03-13 18:00 出处:网络
I\'m working on converting some of the partial views in my MVC 3 app (which I recently converted from MVC 2, using the MS tool) from ascx to cshtml (Razor).

I'm working on converting some of the partial views in my MVC 3 app (which I recently converted from MVC 2, using the MS tool) from ascx to cshtml (Razor).

Simply put: I'm trying to do a Html.RenderPartial of a Razor partial view, directly from an aspx Master Page. The Razor partial view simply has "Hello World" in it -- no @model or @inherits or anything. When I run my app, at this point I get the following error: "The view at .....cshtml must derive from ViewPage, ViewPage, ViewUserControl, or ViewUserControl." In other words, it looks like it's looking for a non-razor view.

So.. If I explicitly put "@inherits ViewUserControl" at the top of my razor partial, I get a different error: "...cshtml.Execute(): No suitable method found to override."

I created a new MVC 3 app from scratch and tried the same thing at its simplest. An aspx master page that does a RenderPartial on a razor partial. And it works fine. But this big hairy existing app is too big to do a start from scratch.. Yet something in there is keeping this from working.

This might look like a duplicate of some other questions out there, but I've tried everything from the ones I've found. I've even uninstalled MVC 2 开发者_JAVA技巧from my machine. Suggestions/help would be most welcome!

Thank you...


Try comparing the two projects to see if there are any significant differences - specifically the Web.config (though maybe settings in the csproj too). Things like the base type used for views/pages are stored in the web.config, so it's possible they're set incorrectly, or even missing from your original project.

A default MVC project has things like this in the web.config in the views folder:

<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">

and

<pages validateRequest="false" pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">


Answering my own question here..

It turns out that the MVC 2 app (which I didn't write, btw) used a custom ViewEngine, which derived from WebFormViewEngine. When I converted my partial view to razor, I added "~/Views/Shared/Modules/{0}.cshtml" to the locations array in that class, without realizing that it'd be using the WebFormViewEngine.

So, I ended up creating my own ViewEngine, which derives from RazorViewEngine... and added my /Modules path to that one instead. It works!

Starting over fresh and manually converting the app to MVC 3 helped (thanks again Darin D), because it forced me to go through all the steps myself, and caused me to find this issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消