开发者

MVC 3 controller action not working

I have working application in asp.net MVC3. Today when I was working on it in VS2010 I found that n开发者_如何学Goone of the action from particular controller are firing. On browsing controller/action I get page not found message. I checked all pages are present (controller, view). Even if I add new action to this controller it is not being called at all. At the same time actions from other controllers are working fine. I can access pages. It's quite weird and I'm not able to figure out it. Any help?

Edit:

Controller-action which is not working is http://localhost:7400/Registration/MedicalHistory/0

Code from global.asax

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        // Home and error page
        routes.MapRoute("error", "error", new { controller = "Home", action = "Error" });

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

        //Admin routing
        routes.MapRoute(
             "Admin", // Route name
             "{controller}/{action}/{id}", // URL with parameters
             new { controller = "Program", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
             new string[] { "ASPNETMVCApplication.Areas.Admin.Controllers" }
        );            
    }

MedicalHistory action:

   [HttpGet]
    public ActionResult MedicalHistory(int id = 0)
    {
      //some code
      return View()
    }


Well first things first you seem to have set up your admin area incorrectly. To add an area you should have in the root of your MVC project a folder structure Areas/{the name of your area}, e.g. Areas/Admin which contains all the usual Controllers, Views folders.

Inside there you should create a class that inherits AreaRegistration and implements at least the AreaName property and RegisterArea() method.

In Global.asax.cs in your Application_Start() handler the first line should be AreaRegistration.RegisterAllAreas();.

Also it is well worth adding RouteDebug to your app and adding a web.config switch you use in you Application_Start() to either turn route debugging on or off. This is absolutely invaluable when your routes are misbehaving or not getting the action parameters you expect.


My guess is that this is a routing issue. Check that your routing isn't mucking up the URL format you're expecting to work.

Can you show us your routing code and also the URL structures you're expecting to work. I may be able to give a more detailed answer with that information.


  • it could be possible your Controller/Action Name is not consistent with the incoming request.
  • Debug and verify the @Request object properties might help as well!!
  • you can also visualise your each request and which routes are maching using this below tool..

http://mvcroutevisualizer.codeplex.com/


I just had a similar problem after renaming a controller. Took me a while to look at the controller's view folder. Totally forgot to rename it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜