简体   繁体   English

ASP.NET MVC 2中是否存在cshtml文件?

[英]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. 我目前正在学习ASP.NET MVC,并研究显示和编辑器模板的概念。 I am working through a post by Scott Mitchell about Using Templates to Display Boolean Values as Yes/No Options . 我正在阅读Scott Mitchell的一篇有关使用模板将布尔值显示为Yes / No Options的帖子。 In it Scott uses .cshtml files within the Shared Folder to store reusable interface templates. 在其中,Scott使用共享文件夹中的.cshtml文件存储可重用的界面模板。

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? 我找不到在Visual Studio 2008 / ASP.NET MVC 2中创建.cshtml文件的任何方法。它们是否仅是ASP.NET MVC 3? Would there be a way to build custom templates without upgrading to ASP.NET MVC 3? 是否可以在不升级到ASP.NET MVC 3的情况下构建自定义模板?

cshtml files are razor views. cshtml文件是剃刀视图。 The Razor view engine is part of Asp.NET MVC 3 (Visual Studio 2010 only), yes. 是的,Razor视图引擎是Asp.NET MVC 3(仅Visual Studio 2010)的一部分。

More Information: Introducing “Razor” – a new view engine for ASP.NET 更多信息: 引入“剃刀”-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. 类似的SO Post 这里是一篇博客文章 ,可以帮助您使Razor使用MVC2。请注意,这是从2010年开始的。至少有几个第三方视图引擎,包括NHAML和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 我相信这些可以与MVC 2一起使用。这是一个链接,其中包括: 视图引擎比较, Stephen Walther网站上的NHAML

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. 经过一些研究,并阅读了@dknaack和@ashok的一些链接,我发现我仍然可以使用MVC 2 View用户控件(即.ascx文件)在MVC 2中创建自定义模板。 A comparable .ascx file to the .cshtml in Scott's blog would look like this: 与Scott博客中的.cshtml类似的.ascx文件如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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