http://tqcblog.com/2011/01/22/asp-net-mvc-3-razor-views-in-sharepoint/

ASP.NET MVC 3 has just been released, and I really like the new Razor syntax for views. However, I work with SharePoint, which is solidly based on WebForms and .NET 3.5. Razor requires .NET 4, and while you can host WebForms MVC pages in _layouts, it’s an ugly hack that gives you the worst features of both systems.

Razor is internally quite different from WebForms, and the core functionality doesn’t actually require MVC, so it should be possible to use it in a system that can’t use MVC.

My first attempt to make Razor work under SharePoint was to see if it would compile without .NET 4. That didn’t work, but I did notice that most of the dependencies on .NET 4 are in parsing the templates - you can’t build cshtml files without .NET 4, but the generated code doesn’t require anything special.

Starting with the code from http://razorengine.codeplex.com/ I was able to convert a cshtml file into a cs file that can be compiled in a .NET 3.5 project. My version of the code is athttps://github.com/tqc/RazorEngine.

RazorEngine came pretty close to what I needed, only requiring fairly minor changes:

* Add methods to return the generated source code rather than compiling and running it

* Copy the template base classes into a .NET 3.5 library (RazorEngine.Run). This meant removing support for dynamic objects, but apart from that the code doesn’t require the latest version.

I set up a console app (GenerateViewCode) that will generate code files for all cshtml files in a project. I considered using a single file generator, but a console app is easier to debug and more likely to work properly on a build server. It is intended to be set up as an external tool in Visual Studio:

* Command: GenerateViewCode.exe

* Arguments (base class for views): RazorEngine.Templating.TemplateBase

* Initial Directory: $(ProjectDir)

After you run the tool on a project containing cshtml files and include the generated code in the project, you will be able to render the view from your code:

var view = new TestViewLibrary.Views.Shared.DocView()
view.Model = newTestViewLibrary.Models.DocSection();
view.Execute();
ParentControl.Controls.Add(new LiteralControl(View.Result));

This replaces either trying to build html in code (with code that looks a lot like the generated output from Razor) or finding and loading a usercontrol in _layouts.

ASP.NET MVC 3 Razor Views in SharePoint的更多相关文章

  1. ASP.NET MVC:Razor 引入命名空间

    原文:ASP.NET MVC:Razor 引入命名空间 页面中引用 c# @using MvcApplication83.Models @using MvcApplication83.Common 行 ...

  2. ASP.NET MVC 自定义Razor视图WorkContext

    概述 1.在ASP.NET MVC项目开发的过程中,我们经常需要在cshtml的视图层输出一些公用信息 比如:页面Title.服务器日期时间.页面关键字.关键字描述.系统版本号.资源版本号等 2.普通 ...

  3. ASP.NET MVC (Razor)开发

    ASP.NET MVC (Razor)开发 过去我们使用过一些周报工具来完成项目组或部门的周报填写与考核工作,但多少有些不理想,要么功能太过简单,要么功能特别繁杂,不接地气,使用不便. 后来我们就考虑 ...

  4. ASP.NET MVC 3: Razor中的@:和语法

    原文 ASP.NET MVC 3: Razor中的@:和语法 [原文发表地址] ASP.NET MVC 3: Razor’s @: and <text> syntax[原文发表时间] De ...

  5. ASP.NET MVC (Razor)开发<<周报与绩效考核系统>>,并免费提供园友们使用~~~

    过去我们使用过一些周报工具来完成项目组或部门的周报填写与考核工作,但多少有些不理想,要么功能太过简单,要么功能特别繁杂,不接地气,使用不便. 后来我们就考虑自己开发一个简单的,实用的,易用的,接地气的 ...

  6. Asp.net mvc 5 razor

    一开始学习dotnet的web项目是Asp.net webform,完全不理解项目为什么要这样设计,就简单的使用ajax调用后台的代码不好吗?为什么还要搞一些什么代码后置的东东. 还有就是有各种加载问 ...

  7. ASP.NET MVC 利用Razor引擎生成静态页

    实现原理及步骤: 1.通过ViewEngines.Engines.FindView查找到对应的视图,如果是部分视图,则用:ViewEngines.Engines.FindPartialView: 2. ...

  8. 实战:ASP.NET MVC中把Views下面的视图放到Views文件夹外

    园子里写的文章的都是把控制器从传统的项目中的Controllers拿出来单独放,但很少几乎没有把视图从Views拿出去这样的文章,今天来写一个. 其实很简单!一步步解决问题就行了,下面记录如下,供需要 ...

  9. ASP.NET MVC 3 Razor 视图引擎 基本语法

    本篇博文将进入MVC 3 的世界了,首先学习一下MVC 3 新增的Razor视图引擎的基本语法. 1. 使用 @ 字符将代码添加到页面中.正如传统的aspx视图的<% %>相同.      ...

随机推荐

  1. oracle的sql函数

    只读事务set transaction read only当一个用户添加了只读事务,则查询时只会查到设置只读事务之前的内容,在并发量大的系统中,通过设置只读事务 便于统计 oracle的sql函数的使 ...

  2. posix thread条件变量

    概述 等待条件变量总是返回锁住的互斥量. 条件变量的作用是发送信号,而不是互斥. 与条件变量相关的共享数据是“谓词”,如队列满或队列空条件. 一个条件变量应该与一个谓词相关.如果一个条件变量与多个谓词 ...

  3. [转]oracle odp.net 32位/64位版本的问题

    本文转自:http://www.cnblogs.com/yjmyzz/archive/2011/04/19/2020793.html 如果你的机器上安装了odp.net,且确信machine.conf ...

  4. Genymontion安装

    Genymontion是什么 它提供的是Android的虚拟环境,和sdk的模拟器类似,能够支持windows,liunx,mac等操作系统,他比我们SDK的模拟器快很多,所以也是为什么很多开发人员会 ...

  5. hihocoder 1186

    1186 : Coordinates 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Give you two integers P and Q. Let all div ...

  6. [Laravel]配置路由小记

    Laravel:4.2 使用的后台是:laravel-backend php artisan routes 使用这个代码,可以看到显示目前项目的路由器 ,我需要添加功能,我就需要添加路由 /* |-- ...

  7. JAXB - XML Schema Types, Defining an Enumeration

    If you want a data type that enumerates discrete values you should use a restriction of the schema t ...

  8. C#学习笔记15:字符串、文件、目录的操作方法

    字符串:不可变性 String str=”abcdf”; 将字符串转换为char数组:ToCharArray(); Char[] ch=str.ToCharAarray(); 将char数组转换为字符 ...

  9. C#学习笔记3:提示“截断字符串或二进制数据”错误解决方法

    1.调试程序如出现“截断字符串或二进制数据”的关于数据库的错误,可以先试一试修改数据库中字符定义的长度. 2.使用ManualResetEvent前需导入 命名空间System.Threading; ...

  10. ASP.NET 上传图片添加文字、Logo水印

    http://www.cnblogs.com/xvqm00/archive/2010/06/22/1762783.html