【问题标题】:Mono MVC 2 home route doesn't workMono MVC 2 主路由不起作用
【发布时间】:2010-10-08 15:23:36
【问题描述】:

我正在尝试将 ASP .NET MVC 2 应用程序转换为在 nginx/mono 2.8 上运行。到目前为止,它似乎工作得很好,除了当路径为空时默认路由不起作用。我将所有请求代理到 fastcgi 服务器,并得到一个 ASP .NET 404 not found 页面。

即这不起作用

http://mysite.com

但这确实

http://mysite.com/home

我的 Global.asax.cs 文件如下所示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MyProject
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            // Default route
            routes.MapRoute(
                "Default",                      // Route name
                "{controller}/{action}/{id}",   // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new string[] {"MyProject.Controllers"}
            );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
        }
    }
}

编辑: 有关我的设置的更多信息。如果这有什么不同,我正在运行 OS X 10.6。 MVC项目中任何区域的默认路由也存在同样的问题。

【问题讨论】:

  • 这样的路线有帮助吗? routes.MapRoute("Root","/",new { controller = "Home", action = "Index"})
  • 是的,添加这样的特定路由确实有效,但我宁愿不这样做,因为 IIS 和 MVC 不需要它。我不确定是 Mono 还是我的 Nginx 设置有问题。
  • 同意。这不是你想做的事。

标签: asp.net-mvc-2 routing mono nginx


【解决方案1】:

我实际上遇到了同样的问题并完全错误地解决了它(至少在我的情况下)......

在 mono 项目网站上的 nginx walkthrough 中,它说在你的 nginx.conf 文件中输入这些行:

index index.html index.htm default.aspx Default.aspx;
fastcgi_index Default.aspx;

嗯,我在两台虚拟机上以完全相同的方式(或者我认为)进行了设置。问题是,一个虚拟机有它的根 URL 工作,而一个没有。结果是我忘记了 VM 上有效的“index”行上的分号,因此“fastcgi_index”行被解释为“index”行的一部分。

因此,在不起作用的 VM 上,我删除了那个分号。你猜怎么着?有效。然后我添加了分号并完全删除了“fastcgi_index”行,它仍然有效。所以基于这个轶事证据和一些猜测工作,我会说'fastcgi_index'行不应该包含在MVC应用程序中。好吧,至少 MVC 3,我还没有测试过其他任何东西。

【讨论】:

    【解决方案2】:

    您是否按照此页面的 nginx 配置进行操作?:http://www.mono-project.com/FastCGI_Nginx

    我的猜测是默认文档会妨碍您。

    【讨论】:

    • 这完全是愚蠢的,但是如果你在路由 url 中添加一个斜线开始:“/{controller}/{action}/{id}”。考虑到@Alex 的建议在技术上是可行的,我能想到的唯一一件事,而且您的区域默认路由也不起作用(这意味着您的默认文档不应该成为问题)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 2013-02-23
    相关资源
    最近更新 更多