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视图的<% %>相同. ...
随机推荐
- Adroid_Spinner_ArrayAdapter
XML布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln ...
- Unix 网络编程(2)——TCP API
TCP C/S套接口函数一般调用过程及基本函数 如上图所示的TCP连接的基本过程.一般来说,服务器先于客户端运行,服务器程序运行的基本过程是: socket()函数创建服务器段socket. bind ...
- SQL Server 内存管理
windows memory: Memory: Cache Bytes 是系统的working set, 也就是系统使用的物理内存数目. 可以观察Windows用了多少物理内存. 1. System ...
- aggregation 详解3(bucket aggregation)
概述 桶分聚合不进行权值的计算,他们对文档根据聚合请求中提供的判断条件(比如:{"from":0, "to":100})来进行分组(桶分). 桶分聚合还会额外 ...
- ubuntu14.04LTS更新源
这两天一直在使用Linux系统做一些事情,但是又会有特别多的报错,其中有一个问题就是源的问题,我知道有太多太多的人写这个源更新的帖子,我现在也写一篇关于源更新的帖子,只是针对ubuntu14.04LT ...
- 分析Redis架构设计
http://blog.csdn.net/a600423444/article/details/8944601 一.前言 因为近期项目中开始使用Redis,为了更好的理解Redis并应用在适合的业务场 ...
- ASP过滤HTML标签
<% Function RemoveHTML(strHTML) Dim objRegExp, Match, Matches Set objRegExp = New Regexp objRegEx ...
- leetcode之Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- Java多线程--同步函数
/*需求:银行有一个金库有两个储户分别存300元 每次存100元,存3次 目的:该程序是否有安全问题,如果有,如何解决? 如何找问题(很重要)1.明确哪些代码是多线程运行代码2.明确共享数据3.明确多 ...
- ZOJ 1122 Clock(模拟)
Clock Time Limit: 2 Seconds Memory Limit: 65536 KB You are given a standard 12-hour clock with ...