使用Asp.Net Core时,如何查看为Razor Page生成的代码?
发布时间:2020-12-16 04:14:09 所属栏目:asp.Net 来源:网络整理
导读:使用Asp.Net,很容易看到Razor View Engine生成的代码:添加编译错误,错误页面将提供对Razor页面源的访问. 这改变了Asp.Net Core,我在某处读到了在内存中创建代码并且不允许轻松访问该代码. 问:有人知道如何使用Asp.Net Core访问生成的Razor源代码吗? 解决
使用Asp.Net,很容易看到Razor View Engine生成的代码:添加编译错误,错误页面将提供对Razor页面源的访问.
这改变了Asp.Net Core,我在某处读到了在内存中创建代码并且不允许轻松访问该代码. 问:有人知道如何使用Asp.Net Core访问生成的Razor源代码吗? 解决方法
将以下类添加到ASP.NET Core MVC项目中:
using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.Razor; using Microsoft.AspNetCore.Mvc.Razor.Compilation; using Microsoft.AspNetCore.Mvc.Razor.Internal; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; public class CustomCompilationService : DefaultRoslynCompilationService,ICompilationService { public CustomCompilationService(ApplicationPartManager partManager,IOptions<RazorViewEngineOptions> optionsAccessor,IRazorViewEngineFileProviderAccessor fileProviderAccessor,ILoggerFactory loggerFactory) : base(partManager,optionsAccessor,fileProviderAccessor,loggerFactory) { } CompilationResult ICompilationService.Compile(RelativeFileInfo fileInfo,string compilationContent) { return base.Compile(fileInfo,compilationContent); } } 使用上面的类重写MVC添加的ICompilationService; public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddSingleton<ICompilationService,CustomCompilationService>(); } 在CustomCompilationService的Compile方法上设置断点并查看compilationContent. 笔记 查看查找区分大小写.如果您的控制器路由寻找名为Index(Index.cshtml)的视图但您已命名视图文件索引(index.cshtml),则会收到异常:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-3 – MVC按钮点击动作
- asp.net-mvc-3 – 将复选框绑定到MVC中的int数组/枚举
- asp.net-mvc – 如何使用MVC 3.0创建Web API
- 在ASP.Net的URL中检索锚点链接
- 在ASP.NET中的JQuery FullCalendar实现
- asp.net – 是否可以在没有Javascript的情况下进行客户端验
- asp.net-mvc – jQuery $.get调用Controller中的方法
- asp.net-mvc – 在哪里?Json.Encode或@Json.Decode方法在M
- asp.net – 基类包含字段’X’,但其类型(System.Web.UI.Scr
- asp.net – 如何使用MVC属性验证列表是否具有大于零的非nul
推荐文章
站长推荐
- asp.net-mvc – MVC反伪造令牌如何在Web服务器重
- asp.net-web-api – Net Web API – 如何在Get上
- Asp.Net Web API跨层传递用户
- asp.net-mvc – 如何在MVC 5A应用程序中实现OWIN
- 一步一步创建ASP.NET MVC5程序[Repository+Autof
- asp.net-mvc – 如果使用HTML内容,我可以写入内联
- asp.net core webapi 统一处理返回值、异常和请求
- asp.net-mvc – MailDefinition / MailMessage和
- asp.net-mvc-3 – 为什么我的textBoxFor使用我的
- ASP.NET的友好URL
热点阅读