How to transition from ASPX to the razor view engine in ASP.NET MVC 3?
I have an ASP.NET MVC 2 web site that I've already upgraded to MVC 3. I'm not finding it very easy for new Razor views to coexist with my older ASPX views. I've found some articles, but I've failed at all of their suggestions and it seems not many people 开发者_JAVA百科have been successful at doing this.
Has Microsoft made anything to make it easier to add Razor support to an upgraded MVC 2 app?
I haven't done this myself, however in this post by Scott Guthrie, there's this screenshot:
Given that this setting exists on the View, it suggests that you can have multiple views using different view engines. This means you should be able to run your existing code as normal, but add new views using Razor.
You can always test this out with a new project, and try adding two views - one using ASPX and one using Razor. If it all works, try diffing the test project to your existing one (specifically things like project and .config files).
You can use this tool from Telerik: https://github.com/telerik/razor-converter to convert your ASPX to CSHTML
The following steps worked for me.
- Create another Temporary project based on MVC 3 Razor support by default
- Compare the references, web.config (main one and one under views folder) in the Temporary project with my own project. Here I found references to System.Web.Helpers & System.Web.Webpages. Similar mentions exist in the web.config files also. Just copy those sections from the temporary project to your project.
- Copy the _ViewStart.cshtml file from temporary project to your project (under Views folder)
- Copy the _Layout.cshtml file from temporary project to your project (under Views\Shared folder)
- Also check inside _Layout.cshtml. It may be referencing other partial views, jquery (js file) etc. Make sure they all exist in your own project.
- That's it. Compile and all should be set right.
精彩评论