Can we use Razor on an existing ASP.NET 4 website?

后端 未结 3 1319
生来不讨喜
生来不讨喜 2021-01-02 03:47

Is it possible to use Razor on an existing ASP.NET 4 website?

3条回答
  •  余生分开走
    2021-01-02 04:09

    marcind is correct, if you want to open your existing ASP.NET website in WebMatrix and work on it from within the tool. If, on the other hand (or in addition to), you want to use Razor syntax in your site and stay within VisualStudio, check out this article: http://weblogs.asp.net/rashid/archive/2010/07/10/use-razor-as-asp-net-mvc-viewengine.aspx

    There are four things you need to do:

    1. Add References to the Razor assemblies installed with WebMatrix. These can be found at C:\Program Files\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies

    2. Create a custom ViewEngine class, a View class that inherits from IView (not that hard, check out the source in the article above)

    3. Add your new ViewEngine in Global.asax Application_Start()

      ViewEngines.Engines.Add(new RazorViewEngine(("cs"));
      
    4. Create your view pages with a .cshtml extension, instead of .aspx

    There are a few steps here, but it's quick work, and the source from the article above will get you a long way there.

提交回复
热议问题