I have a site that uses Ninject for dependency injection and I have Routing defined within a Bootstrapper class like so:
public void RegisterRoutes()
{
Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
Routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?favicon.ico(/.*)?" });
Routes.MapRoute(
null,
"{pageTitle}",
new { controller = "Home", action = "Page", pageTitle = "Index" }
);
Routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
I have added an Area to the project but the default AdminAreaRegistration is not registering the root
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Where or how do I register the Area with Ninject?
The sample project comming with the source code has now an area. Take a look at it. https://github.com/ninject/ninject.web.mvc/zipball/master
are you calling RegisterAllAreas()?
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
note it must be called before RegisterRoutes().
Have you resolved this issue?
I have the problem where my NinjectControllerFactory is not resolving urls that reference controllers defined in areas. I get the following message:
The IControllerFactory 'myWebSite.WebUI.Infrastructure.NinjectControllerFactory' did not return a controller for the name 'admin'.
If I move the controller to the root Controllers folder, it will resolve the url.
Related
I have some problems using Areas in my MVC project. I'm able to access my controller, which is located under the area, but when it returns to view (with model), I get an error:
The view 'Index' or its master was not found or no view engine
supports the searched locations.The following locations were searched:
~/Views/MyController/Index.aspx ~/Views/MyController/Index.ascx etc.
Here is the MyAreaAreaRegistration:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"MyArea_default",
"MyArea/{controller}/{action}/{id}",
new { controller = "MyController", action = "Index",
id = UrlParameter.Optional });
}
Routeconfig:
routes.MapRoute(
name: "Default", // Route name
url: "{controller}/{action}/{id}", // URL with parameters
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyApp.Controllers" }
);
Global.asax:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
...
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
And controller:
return View(myViewModel);
I'm totally stuck with this one. Any help would be much appreciated.
Please change your route config code .
routes.MapRoute(
name: "Default", // Route name
url: "{controller}/{action}/{id}", // URL with parameters
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
).DataTokens.Add("area", "MyArea"); ;
It will be work for you .
Thanks .
I created two areas (with controllers, views, registration, etc) with VS's scaffolding process. One for homecontroller and one called account.
I added a single line "route.DataTokens" to the routeconfig.cs file to connect to the homecontroller on startup.
var route = routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional }
);
route.DataTokens["area"] = "Home";
Here is my HomeAreaRegistration.cs RegistrationArea method
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
But in the url of the browser I see
https://localhost:44301/Home/Home/Index
What I want to see is
https://localhost:44301/Home/Index
What do I modify to get this?
Edit - Solved! I think.
Here is what I did.
In HomeArearegistration.cs I removed the Home/
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default",
"{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
Is this the proper way to go about doing this because something doesn't seem right here, if I type
https://localhost:44301/Home/Index.cshtml
which I would expect to take me to the home page, I get a HTTP 404 error not found but if I type
https://localhost:44301/Home/Index
It take me to the home page
I have an MVC 4 web application which consists some areas. I have a problem with the routing rules of an area named "Catalog". The RouteConfig.cs file is:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
);
}
and Global.asax as follows:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
And CatalogAreaRegistration is something like this:
public class CatalogAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Catalog";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Catalog_default",
"Catalog/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
The problem is when i debug, RouteCollection routes does not include rules that are defined in the area. I used routedebugger and saw that routes collection does not consists rules of "Catalog" area. It has only rules in the RouteConfig.
I have no idea what is the problem. Thanks in advance.
I think due to Visual Studio's caching, some of the dll's aren't compiled properly and this situation can happen. If you do, delete all temp files from following locations:
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
Update :
AppData\Local\Temp\Temporary ASP.NET Files
Then restart the Visual Studio. This is how i resolved.
Just add the namespace of your controllers to the AreaRegistration:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
**namespaces: new string[] { "Web.Admin.Controllers" }**
);
}
A route named 'Home_default2' is already in the route collection. Route names must be unique.
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
AreaRegistration.RegisterAllAreas();
routes.MapRoute(
name: "Default",
url: "area/{controller}/{action}/{id}",
defaults: new {area="Home_Default", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
public override string AreaName
{
get
{
return "Home";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default2",
"Home/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
The auto generated code is bugging, what I did wrong?
To solve this problem, just delete all the .dll files in your bin folder and then build the solution again. This should solve the problem for you.
The problem is a duplicate `AreaRegistration.RegisterAllAreas(); on route and on global.asax
so need only this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
Changing Home_Default to Home.
In my case, I had created an area & I had added this line in Route.config.
AreaRegistration.RegisterAllAreas();
But this statement was already present in Application_start of global.asax. hence got the error.
So removed it from route.config.
I did not change any route name for it. one route name was default (in RouteConfig file) & other was areaname_default (in AreaRegistration.cs file).
I tried implementing Phil's Areas Demo in my project
http://haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx.
I appended the Areas/Blog structure in my existing MVC project and I get the following error in my project.
The controller name Home is ambiguous between the following types:
WebMVC.Controllers.HomeController
WebMVC.Areas.Blogs.Controllers.HomeController
this is how my Global.asax looks.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapAreas("{controller}/{action}/{id}",
"WebMVC.Areas.Blogs",
new[] { "Blogs", "Forums" });
routes.MapRootArea("{controller}/{action}/{id}",
"WebMVC",
new { controller = "Home", action = "Index", id = "" });
//routes.MapRoute(
// "Default", // Route name
// "{controller}/{action}/{id}",// URL with parameters
// new { controller = "Home", action = "Index", id = "" }
// // Parameter defaults
//);
}
protected void Application_Start()
{
String assemblyName = Assembly.GetExecutingAssembly().CodeBase;
String path = new Uri(assemblyName).LocalPath;
Directory.SetCurrentDirectory(Path.GetDirectoryName(path));
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new AreaViewEngine());
RegisterRoutes(RouteTable.Routes);
// RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
}
If I remove the /Areas/Blogs from routes.MapAreas, it looks at the Index of the root.
In ASP.NET MVC 2.0, you can include the namespace(s) for your parent project controllers when registering routes in the parent area.
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" },
new string[] { "MyProjectName.Controllers" }
);
This restricts the route to searching for controllers only in the namespace you specified.
Instead of WebMVC.Areas.Blogs and WebMVC, use WebMVC.Areas.Blogs and WebMVC.Areas.OtherAreaName. Think of the area name as the namespace root, not an absolute namespace.
You can prioritize between multiple controllers with the same name in routing as follows
E.g., I have one controller named HomeController in Areas/Admin/HomeController
and another in root /controller/HomeController
so I prioritize my root one as follows:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", // Parameter defaults
id = UrlParameter.Optional },
new [] { "MyAppName.Controllers" } // Prioritized namespace which tells the current asp.net mvc pipeline to route for root controller not in areas.
);