razor - 在 CentOS 上使用 Razor View 运行 ServiceStack

标签 razor centos servicestack mod-mono

我已经从 https://github.com/ServiceStack/RazorRockstars.git 克隆了 RazorRockstars 项目并验证它可以在 Windows 上运行。 现在我想将其部署到带有 Mono 2.10.8 的 Linux CentOS 6.3。我按照这个教程:http://pastebin.com/TBf3NWTw

问题是我无法找到运行 Razor View 的解决方案。

我的 mod_mono.conf 文件如下所示:

<IfModule !mod_mono.c>
  LoadModule mono_module /usr/lib/httpd/modules/mod_mono.so
</IfModule>

<IfModule mod_headers.c>
   Header set X-Powered-By "Mono"
</IfModule>

AddType application/x-asp-net .aspx
AddType application/x-asp-net .cshtml
AddType application/x-asp-net .asmx
AddType application/x-asp-net .ashx
AddType application/x-asp-net .asax
AddType application/x-asp-net .ascx
AddType application/x-asp-net .soap
AddType application/x-asp-net .rem
AddType application/x-asp-net .axd
AddType application/x-asp-net .cs
AddType application/x-asp-net .vb
AddType application/x-asp-net .master
AddType application/x-asp-net .sitemap
AddType application/x-asp-net .resources
AddType application/x-asp-net .skin  
AddType application/x-asp-net .browser
AddType application/x-asp-net .webinfo
AddType application/x-asp-net .resx
AddType application/x-asp-net .licx
AddType application/x-asp-net .csproj
AddType application/x-asp-net .vbproj
AddType application/x-asp-net .config
AddType application/x-asp-net .Config
AddType application/x-asp-net .dll
DirectoryIndex index.aspx
DirectoryIndex Default.aspx
DirectoryIndex default.cshtml
DirectoryIndex default.aspx
MonoServerPath "/opt/mono/bin/mod-mono-server4"

如果我将 Default.aspx 文件添加到/var/www/RazorRockstars 文件夹中,我可以在屏幕上查看主页,但如果我单击链接 (Henrix),我最终会收到一条错误消息,告诉我“在此服务器上找不到请求的 URL/stars/dead/hendrix。”

这可能是一个简单的解决方案。我花了几个小时在谷歌上搜索答案,但没有找到解决方案。

最佳答案

我们使用Nginx/FastCGIMono to server mono ASP.NET sites ourselves ,尽管您不需要对 razor 文件扩展名映射进行任何特殊操作来处理 mod_mono 配置层中的 razor 文件,但您所需要做的就是确保请求通过 ServiceStack ASP.NET 主机。

配置它,以便请求传递到 ServiceStack

目标应该只是确保请求通过 ServiceStack 进行处理(ServiceStack 将在到达后处理其余部分),因此您应该通过指定任何可能阻止它的中间件层配置来避免阻碍它。

ServiceStack 的 Nginx + FastCGI Mono 配置用于 razor.servicestack.net

这是我们用于托管的 ServiceStack 的 nginx 配置 razor.servicestack.net使用 Nginx(在 Ubuntu 上),它位于 /etc/nginx/sites-available/servicestack.net 中的单独配置文件中:

server {
    listen 0.0.0.0:80;
    server_name razor.servicestack.net;
    access_log /var/log/nginx/servicestack.net.log;

    root /home/mythz/src/RazorRockstars/src/RazorRockstars.WebHost;

    #get nginx to handle static files for better performance
    location /img/ {
       alias /home/mythz/src/RazorRockstars/src/RazorRockstars.WebHost/img/;
    }    
    location ~* \.(ico|pdf|flv|swf|exe|html|htm|txt|css|js) {
       add_header        Cache-Control public;
       add_header        Cache-Control must-revalidate;
       expires           1d;
    } 

    #proxy all requests to monofastcgi backend + specify supported default documents
    location / {
        index index.html index.htm index.aspx default.htm Default.htm default.aspx Default.aspx Default.ashx default.cshtml;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }
}

这是我们用来指定所有 ASP.NET Web 应用程序的 Mono FastCGI 配置文件,我们将其保存在 /etc/init.d/mono-fastcgi/ServiceStack.webapp 中。 :

<apps>
<web-application>
        <name>RazorRockstars</name>
        <vhost>razor.servicestack.net</vhost>
        <vport>80</vport>
        <vpath>/</vpath>
        <path>/home/mythz/src/RazorRockstars/src/RazorRockstars.WebHost</path>
</web-application>
...
<apps>

最后,这是我们运行的 fastcgi 命令,它是托管 ServiceStack.webapp 中指定的所有 Mono ASP.NET 站点的进程。上图:

/usr/bin/fastcgi-mono-server4 --appconfigdir /etc/init.d/mono-fastcgi \
/socket=tcp:127.0.0.1:9000 /logfile=/var/log/mono/fastcgi.log

--appconfigdir /etc/init.d/mono-fastcgi指令表示托管任何 *.webapp 中包含的所有 ASP.NET Web 应用程序FastCGI Mono 配置文件位于 /etc/init.d/mono-fastcgi目录。

幕后发生了什么

当请求razor.servicestack.net时到达 端口 80 它由 nginx 处理,它与 server_name razor.servicestack.net; 上的第一个 nginx 配置 block 匹配。

location / { .. }指令告诉 nginx 将所有后备路由转发到由 fastcgi_pass 127.0.0.1:9000; 指定的 端口 9000 支持的 fastcgi .

该请求现在由 FastCGI Mono 处理,它将定向到标识为 <vhost>razor.servicestack.net</vhost> 的 ASP.NET Web 应用程序。作为 ServiceStack 处理程序是 mounted at the / root path ,ServiceStack最终处理该请求。

关于razor - 在 CentOS 上使用 Razor View 运行 ServiceStack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16148251/

相关文章:

c# - ServiceStack请求参数还是 session 变量?

c# - asp.net mvc 中的 sitemaps.xml

html - 有没有办法将类型选择器应用于元素?

c# - 为什么元组中的接口(interface)不能用作 ASP.NET Razor 部分 View 中的模型?

javascript - 为什么 @ID 变量在同一 <text></text> 元素内呈现不同的效果

node.js - 缺少 Node.js 的目录

centos - GPG 检查在 CentOS Stream 9 上失败,但在 Fedora 35 上则不然

c# - ServiceStack 心跳更新 session

windows - 指向已安装 Windows 文件夹的符号链接(symbolic link)不起作用

.net - 从 DTSX 包中使用 REST API 的最佳方式是什么?