简体   繁体   中英

Does cshtml files exist in ASP.NET MVC 2?

I am learning ASP.NET MVC currently, and looking at the concept of Display and Editor Templates. I am working through a post by Scott Mitchell about Using Templates to Display Boolean Values as Yes/No Options . In it Scott uses .cshtml files within the Shared Folder to store reusable interface templates.

I can't find any way to create .cshtml files in Visual Studio 2008 / ASP.NET MVC 2. Are they an ASP.NET MVC 3 only thing? Would there be a way to build custom templates without upgrading to ASP.NET MVC 3?

cshtml files are razor views. The Razor view engine is part of Asp.NET MVC 3 (Visual Studio 2010 only), yes.

More Information: Introducing “Razor” – a new view engine for ASP.NET

hope this helps

Similar SO Post here Here is a blog post that may help you get Razor working with MVC 2. Note this is from 2010. There are at least a couple of third party view engines including NHAML and SparkView. I believe that these should work with MVC 2. Here are a coiple of links: comparison of view engines , NHAML at Stephen Walther site

After some research, and reading some of the links from @dknaack and @ashok, I have figured out that I can still create custom templates in MVC 2 using MVC 2 View User Controls, which are .ascx files. A comparable .ascx file to the .cshtml in Scott's blog would look like this:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<% if(ViewData.Model is bool && ((bool)ViewData.Model) == true)
   { %>
   <div>Yes</div>
<%}else{ %>
   <div>No</div>
<%} %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM