ASP.NET MVC] Route match but controller never call

2019-07-22 00:13发布

问题:

Hi everyone I have a problem with MVC3 routing and areas:

My route matches but the controller is never called, instead i get a 404 error.

I have set an MVC3 solution with 2 project:

  • One is the main MVC Project: CityServices
  • One for an area: CityServices.Demo

In my area I register the following route:

public class TestAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Test";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Test_default",
            "Test/{controller}/{action}",
            new { controller = "Stuff", action = "ListAll" }
        );
    }
}

Content of StuffController.cs:

public class StuffController : Controller
{
    public ActionResult ListAll()
    {
        List<Stuff> lstStuff = new List<Stuff>
                                   {
                                       new Stuff()
                                           {
                                               Id = 0,
                                               Name = "HEeey",
                                               Value = 10.456f
                                           },
                                       new Stuff()
                                           {
                                               Id = 1,
                                               Name = "Beeee",
                                               Value = 456789.47879999f
                                           },
                                       new Stuff()
                                           {
                                               Id = 2,
                                               Name = "HooAAaoo",
                                               Value = 0f
                                           }
                                   };

        return Json(lstStuff);
    }
}

I register area in the main project:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
        //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
    }

My route is matched: http://host/Test But i always get a 404 error.

Any ideas?

Thanks, - Jeremy

Edit: Here is a proof that the route matches:

Found Solution

So to sum it up. My AreaRegistration inherited class was not in the same namespace as my controllers. Since i did not have any views (it's just a restful websvc) i did not have to use MvcContribs which seems to be useful when you have views&controllers in a differents project than the main site.

Hope this helps if you arrive on this question ;)

回答1:

You have your area in another project which won't work per default unless you are using MvcContribs portable areas or another custom implementation



回答2:

I think you need to set the allow get for json:

return Json(data, JsonRequestBehavior.AllowGet)