MVC 3 Area route not working

前端 未结 8 732
有刺的猬
有刺的猬 2020-12-17 18:54

I created a Area in my MVC 3 application called \'Blog\'.

In global.asax I have the following code.

public static void RegisterRoutes(RouteCollection         


        
相关标签:
8条回答
  • 2020-12-17 19:25

    I had the same issue as Mindstorm Interactive, and took a slightly different approach. Yes, it does redirect the user, but it made my code look less workaroundy so to speak.

    I created a new controller, no matter in what area, since the problem is within the renderer not finding the view, not the controller.

    The controller then redirects with the area (as in Mindstroms fix, the key) included, and voila.

    public class StartController : Controller
    {
      public ActionResult Index()
      {
        return RedirectToAction("Index", "MyController", new { area = "MyArea" });
      }
    }
    

    in the RouteConfig just add

    routes.MapRoute(name: "Root", url: "", defaults: new { controller = "Start", action = "Index", area = "MyArea" });
    

    Hope it helps someone.

    0 讨论(0)
  • 2020-12-17 19:33

    The registration in your area appears to be wrong. You specify a default for your action but not for the controller. Since you typically have Home as the name of the controller you'd need to specify that.

    Also it could be you don't have your folders setup correctly since you should have physically setup:

    • /Areas/Blog
    • /Areas/Blog/Controllers
    • /Areas/Blog/Views

    ... and once you have fixed your blog area route you'll also need:

    • /Areas/Blog/Views/Home << Put your index view in here

    The error you get seems to pretty clearly indicate this is the issue.

    0 讨论(0)
提交回复
热议问题