MVC 4 Area Routing is not working

后端 未结 6 990
误落风尘
误落风尘 2021-02-14 07:39

I created an empty MVC4 Application, all things are working fine, after that I add an Area to my Project Named \"Moderator\". My area routing code is like this:



        
相关标签:
6条回答
  • 2021-02-14 08:13

    I had the same problem. You look to App_Start/RouteConfig.cs and add top this code AreaRegistration.RegisterAllAreas();

    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            AreaRegistration.RegisterAllAreas();
    
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
    

    Create create Areas/Moderator/Controllers/DashboardController.cs

    public class DashboardController : Controller
    {
        //
        // GET: /Moderator/Dashboard/
    
        public ActionResult Index()
        {
            return View();
        }
    }
    

    and create

    Areas/Moderator/Views/Dashboard/Index.cshtml

    You also need to have Web.config in Areas/Moderator/Views/Web.config ...

    0 讨论(0)
  • 2021-02-14 08:13

    Just try to delete content from following directories as mentioned here and rebuild project

    C:\Temp C:\Users\%Username%\AppData\Local\Microsoft\VisualStudio C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files Path\To\Your\Project\obj\Debug

    0 讨论(0)
  • 2021-02-14 08:16

    Its better to add

    AreaRegistration.RegisterAllAreas();

    in global.asax.cs. You must place in following order.

    GlobalConfiguration.Configure(WebApiConfig.Register);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    

    Note one things that RegisterAllAreas() should be after Registering the routes. ie. RouteConfig.RegisterRoutes(RouteTable.Routes); should be before AreaRegistration.RegisterAllAreas(); .I have tried and tested and it worked for me.

    For more info: enter link description here

    0 讨论(0)
  • 2021-02-14 08:26

    Just add the "namespace:" named parameter in RegisterRoutes() method of RouteConfig.cs in App_Start folder. And specify the value of "namespace" to the namespace of your controller, within which you want to call action method. In following example i want to call Index() method in Village Controller from root of project.

    public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Village", action = "Index", id = UrlParameter.Optional },
                namespaces: new string[] { "MvcApplication1.Controllers" }
            );
        }
    
    0 讨论(0)
  • 2021-02-14 08:27

    Check the namespace of your controller.

    I had moved some code into an area and the namespace was still showing the old location.

    ReSharper has a very cool option to fix the namespaces of all files in the folder, project or solution! It is very handy to fix this.

    0 讨论(0)
  • 2021-02-14 08:35

    Make sure that in project properties > Web > Start Action > if a specific page | action | url is defined, it does match with mapped routes.

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