ASP.NET MVC 3 Razor Views in SharePoint
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的更多相关文章
- ASP.NET MVC:Razor 引入命名空间
原文:ASP.NET MVC:Razor 引入命名空间 页面中引用 c# @using MvcApplication83.Models @using MvcApplication83.Common 行 ...
- ASP.NET MVC 自定义Razor视图WorkContext
概述 1.在ASP.NET MVC项目开发的过程中,我们经常需要在cshtml的视图层输出一些公用信息 比如:页面Title.服务器日期时间.页面关键字.关键字描述.系统版本号.资源版本号等 2.普通 ...
- ASP.NET MVC (Razor)开发
ASP.NET MVC (Razor)开发 过去我们使用过一些周报工具来完成项目组或部门的周报填写与考核工作,但多少有些不理想,要么功能太过简单,要么功能特别繁杂,不接地气,使用不便. 后来我们就考虑 ...
- ASP.NET MVC 3: Razor中的@:和语法
原文 ASP.NET MVC 3: Razor中的@:和语法 [原文发表地址] ASP.NET MVC 3: Razor’s @: and <text> syntax[原文发表时间] De ...
- ASP.NET MVC (Razor)开发<<周报与绩效考核系统>>,并免费提供园友们使用~~~
过去我们使用过一些周报工具来完成项目组或部门的周报填写与考核工作,但多少有些不理想,要么功能太过简单,要么功能特别繁杂,不接地气,使用不便. 后来我们就考虑自己开发一个简单的,实用的,易用的,接地气的 ...
- Asp.net mvc 5 razor
一开始学习dotnet的web项目是Asp.net webform,完全不理解项目为什么要这样设计,就简单的使用ajax调用后台的代码不好吗?为什么还要搞一些什么代码后置的东东. 还有就是有各种加载问 ...
- ASP.NET MVC 利用Razor引擎生成静态页
实现原理及步骤: 1.通过ViewEngines.Engines.FindView查找到对应的视图,如果是部分视图,则用:ViewEngines.Engines.FindPartialView: 2. ...
- 实战:ASP.NET MVC中把Views下面的视图放到Views文件夹外
园子里写的文章的都是把控制器从传统的项目中的Controllers拿出来单独放,但很少几乎没有把视图从Views拿出去这样的文章,今天来写一个. 其实很简单!一步步解决问题就行了,下面记录如下,供需要 ...
- ASP.NET MVC 3 Razor 视图引擎 基本语法
本篇博文将进入MVC 3 的世界了,首先学习一下MVC 3 新增的Razor视图引擎的基本语法. 1. 使用 @ 字符将代码添加到页面中.正如传统的aspx视图的<% %>相同. ...
随机推荐
- ios运行某些工程时屏幕上下出现黑边的解决办法
今天准备了解下MVVM设计模式,于是就从GitHub上Down了一个MVVM的demo(地址在这)学习,下载之后,在模拟器上运行一下,出现如下图上下有黑边,以前也遇到过这个问题,但当时没有记录,现在还 ...
- 关于JFace中的输入值(InputDialog)对话框类
格式: InputDialog(Shell parentShell, String dialogTitle, String dialogMessage, String initialValue, In ...
- 关于SWT的容器类之----面板Composite类和Group类
1.Comosite类谱系图. Composite的用法: 格式:Composite(Composite parent,int style) 用法:Composite composite = new ...
- Android开发了解——ODEX
ODEX是安卓上的应用程序apk中提取出来的可运行文件,即将APK中的classes.dex文件通过dex优化过程将其优化生成一个·dex文件单独存放,原APK中的classes.dex文件会保留.这 ...
- JAXB - XML Schema Types, Defining Subtypes
Although object orientation isn't a key feature of XML or the XML Schema language, it's still possib ...
- 阻止子View获取焦点方法
android:descendantFocusability:ViewGroup ep: android:descendantFocusability=blocksDescendants
- SSRS和SSAS是支持VB的
SSRS和SSAS是支持VB的,而且自定义Code其实也是只支持VB,或者其他语言可以编码成DLL再用咯.下面是官方VB函数库,基本上都能用,保存起来妥妥的. https://msdn.microso ...
- 使用javascript获取gridview中的textbox值
<body> <form id="form1" runat="server"> <div> <asp:gridview ...
- ios 获取字符串所需要占用的label的高度
// 设置字体大小 UIFont *fnt=[UIFont systemFontOfSize:16]; NSDictionary *attribute = @{NSFontAttributeNa ...
- 轮子来袭 vJine.Core Orm 之 02_代码生成
1.下载并安装动软代码生成器: 2.下载vJine.Core.Orm模板: 3.解压模本文件并复制到如下的动软代码生成器模板目录: 4.打开动软代码生成器会看到模板已识别加载: 5.选择模板代码批量生 ...