简体   繁体   English

全局在IIS上提供.cshtml文件

[英]Serving .cshtml files on IIS globally

I am trying to setup intranet IIS 8.5 (Win8.1) to globally serve .cshtml (Razor) files. 我正在尝试将Intranet IIS 8.5(Win8.1)设置为全局提供.cshtml (Razor)文件。 The corresponding Application Pool is set to v4.0 . 相应的应用程序池设置为v4.0

The files are simple Web Pages, not MVC. 这些文件是简单的网页,而不是MVC。 Here is an example of one: 这是一个示例:

<html>
<body>
@foreach (var i = 0; i < 10; i++)
{
   <li>Item @i</li>
}
</body>
</html>

By removing the mapping of .cshtml files to System.Web.ForbiddenHandler on the IIS server's Handler Mappings I was able to get past the initial hurdle of ASP.NET telling me that 通过删除IIS服务器的Handler映射上的.cshtml文件到System.Web.ForbiddenHandler映射,我可以克服ASP.NET告诉我的最初障碍

This type of file is not served. 无法提供此类文件。

However, the .cshtml files are now served verbatim to the browser, instead of being run through the Razor rendering process. 但是,现在将.cshtml文件原样提供给浏览器,而不是通过Razor渲染过程运行。

One would think that it should be easy to serve Razor pages from IIS, but it isn't. 有人认为从IIS服务Razor页面应该很容易,但事实并非如此。 I need to somehow convince IIS to interpret these pages as Razor views; 我需要以某种方式说服IIS将这些页面解释为Razor视图。 I suspect I am missing some mapping to the appropriate handler (I don't want MVC though - just simple Web Pages). 我怀疑我缺少一些到适当处理程序的映射(尽管我不想要MVC-只是简单的Web页)。

Here are some additional constraints: 以下是一些其他约束:

  • I would definitely like to avoid including a bin folder with the requisite Razor assemblies in each of the sites on the server. 我绝对希望避免在服务器上的每个站点中都包含带有必需的Razor程序集的bin文件夹。 The server hosts many sites, and I don't want to have to copy the bin folder everywhere. 该服务器托管许多站点,我不需要到处复制bin文件夹。 It should be possible to configure it globally, once and for all. 应该有可能一劳永逸地全局配置它。

  • Ideally, I would not even need a local web.config for each site. 理想情况下,每个站点我都不需要本地的web.config The sites that are being served are a patchwork of technologies, containing .html , .shtml , .php , .asp , .aspx , and - hopefully - .cshtml files and should not be dependent on a single technology or config. 所服务的站点是技术的拼凑而成,其中包含.html.shtml.php.asp.aspx和-希望.cshtml文件,并且不应依赖于单个技术或配置。

  • Creating a Visual Studio project is expressly out of the question. 创建Visual Studio项目显然是不可能的。 I should be able to use any text editor to modify the .cshtml files. 我应该能够使用任何文本编辑器来修改.cshtml文件。

  • .NET Core is not installed on the machine and is not an option. .NET Core未安装在计算机上,也不是一个选择。 Must use full Framework up to 4.6.2. 必须使用完整的框架,最高为4.6.2。

I am aware of many other SO questions that are similar, but don't quite solve my problem. 我知道许多其他类似的问题,但并不能完全解决我的问题。

This question for example, was closed as "unclear" before it could have been answered, yet it was pretty clear to me! 例如,在可以回答之前, 该问题被关闭为“不清楚”,但对我来说很清楚! I am having the exact same problem. 我现在正在遇到一模一样的问题。

The accepted answer to this question simply resorts to copying the bin folder. 这个问题的公认答案只是求助于复制bin文件夹。 This is something I specifically don't want to do. 这是我特别不想做的事情。

This answer says you can run an MVC application without installing MVC on your server , again by copying a bin folder into the local root. 这个答案说您可以运行MVC应用程序,而无需在服务器上安装MVC ,只需将bin文件夹复制到本地根目录中即可。 I do want to install Razor (but not necessarily MVC) onto my server globally. 确实想在全球范围内将Razor(但不一定是MVC)安装到我的服务器上。

Essentially, what I am trying to do is to use Razor syntax in a way reminiscent of classic ASP, or ASPX, without the baggage of MVC. 本质上,我想做的是使用Razor语法,使人联想到经典的ASP或ASPX,而没有MVC的负担。

Can it be done? 能做到吗

I believe what you're looking for is a feature called ASP.NET Web Pages. 我相信您正在寻找的是一种称为ASP.NET网页的功能。 If you use ASP.NET Core 2.0, the most recent new project templates uses Pages, rather than controllers and views. 如果使用ASP.NET Core 2.0,则最新的新项目模板将使用Pages,而不是控制器和视图。 I've never read this doco , but I guess it should help you get started (or just create an ASP.NET Core 2.0 project from the new project wizard) 我从未阅读过此文档 ,但我想它应该可以帮助您入门(或者只是通过新的项目向导创建ASP.NET Core 2.0项目)。

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

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