I'm trying to convert an ASP .NET MVC 2 app to run on nginx/mono 2.8. So far it seems to work quite well except that the default route doesn't work when the path is empty. I am proxying all requests through to the fastcgi server and I get served up with an ASP .NET 404 not found page.
i.e. This doesn't work
http://mysite.com
But this does
http://mysite.com/home
My Global.asax.cs file looks like this
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);
}
}
}
EDIT: Some more info on my setup. I am running OS X 10.6 if that makes any difference. Also the same problem exists for the default route of any areas in the MVC project.
Every MVC application must configure (register) at least one route configured by the MVC framework by default. You can register a route in RouteConfig class, which is in RouteConfig. cs under App_Start folder.
A route is a URL pattern. Routing is a pattern matching process that monitors the requests and determines what to do with each request. In other words we can say Routing is a mechanism for mapping requests within our MVC application. The Routing mechanism passes the request to the handler.
Every ASP.NET MVC application must configure (register) at least one route in the RouteConfig class and by default, the ASP.NET MVC Framework provides one default route. But you can configure as many routes as you want.
In the custom routing mode MVC sites respond to incoming requests using standard ASP.NET routing. Page URLs are determined by the routes that you register into your MVC application's routing table.
I actually ran into the same problem and solved it (at least in my situation) by complete mistake...
In the nginx walkthrough on the mono project's site, it says to enter these lines in your nginx.conf file:
index index.html index.htm default.aspx Default.aspx;
fastcgi_index Default.aspx;
Well, I set this up in the exact same way (or so I thought) on two VMs. Problem is, one VM had it's root url work and one didn't. What it turned out to be was that I forgot the semi-colon on the 'index' line on the VM that worked, so that the 'fastcgi_index' line was interpreted as part of the 'index' line.
So on the VM that didn't work, I removed that semi-colon. And guess what? It worked. So then I added the semi-colon and entirely removed the 'fastcgi_index' line and it still worked. So based on this anecdotal evidence and some guess work, I'd say that the 'fastcgi_index' line should not be included in MVC applications. Well, at least MVC 3, I haven't tested anything else.
Did you follow the nginx configuration from this page?: http://www.mono-project.com/FastCGI_Nginx
My guess would be the default document is getting in the way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With