I have a MVC 2 site with an area, let's say the area name is {Admin}
The areas and the site works fine.
What I am trying to do is to have different default page for the area.
When I am calling http://webSiteName works with no problem
but for http://webSiteName/Admin I am getting the error
The resource cannot be found
I tried it out the solutions from ASP.NET MVC 2 RC 2 returns Area-specific controller when no area specified
but with no luck.
I tried also
routes.MapRoute(
"Admin",
"{controller}/{action}/{id}",
new { controller = "AdminHome", action = "index" },
new[] { "Web.Areas.Admin.Controllers" }
);
and
routes.MapRoute(
"Admin",
"Admin",
new { controller = "AdminHome", action = "index" },
new string[] { "Web.Areas.Admin.Controllers" }
);
but I am still getting The resource cannot be found.
What am I doing wrong?
Try this. Make sure it will be in /Areas/Admin/AdminAreaRegistration.cs when your Area is named Admin.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "AdminHome",action = "Index", id = "" }
);
}
You don't have to add anything to your Global.asax.
Related
Problem
In asp.net mvc i have two mvc application in one solution one is website and second is admin panel. And I I have created one folder that name is administrator inside of website project and paste admin project inside of website project now I want access admin section like way :
http://localhost/website/admin
As like nopcommerce but I'm not getting how nopcommerce configured this thing in their project.
Help me out
As others have stated, it may be best to use ares in order to simplify the project, but if you want to keep them separated, I think the problem you are having is updating your routing.
By default, MVC applications have the following routing config (found in the Globals.asax.cs file):
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
What you will need to do is look at the controller you're trying to wire up, and put in a route for that controller. For the sake of an example, I will assume your controller is called "AdminController":
routes.MapRoute(
name: "AdminController",
routeTemplate: "website/admin/{action}",
defaults: new { controller = "Admin", action = "Index"}
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
This example shows you how to map the desired route -- http://localhost/website/admin -- to the "Index" action on the "AdminController" object.
For more in-depth ASP.Net routing examples, you can look at the documentation here
Update: After looking at the example library in question (NopCommerce), it would appear they are using an explicit area registration. This is found in 'src/Presentation/Administration/AdminAreaRegistration.cs' :
using System.Web.Mvc;
namespace Nop.Admin
{
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", area = "Admin", id = "" },
new[] { "Nop.Admin.Controllers" }
);
}
}
}
Hopefully this gives you a better idea of how this is done.
This tutorial has cleared my confusion that how to configure more then one project in asp.net mvc as area.
http://nileshhirapra.blogspot.no/2012/02/aspnet-mvc-pluggable-application.html?m=1
I am working on asp.net mvc 3. I am trying to implement the project with areas. i am facing problems with routing. I have folder structure like, projectName/areas/Dashboard/ and in my DashboardAreaRegistration.cs
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Default_Dashboard",
"Dashboard/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
so my routing looks like, localhost:56788/Dashboard/Dashboard/Index here i need my routing should looks like localhost:56788/Dashboard/Index for that i have written like,
context.MapRoute(
"Default_Dashboard",
"Dashboard/{action}/{id}",
new { area = "Dashboard", controller = "Dashboard", action = "PatientPortal", id = UrlParameter.Optional },
new[]{typeof(Controllers.DashboardController).Namespace}
);
context.MapRoute(
"Default_Dashboard",
"{controller}/{action}/{id}",
new { area = "Dashboard", controller = "Dashboard", action = "PatientPortal", id = UrlParameter.Optional },
new[]{typeof(Controllers.DashboardController).Namespace}
);
but no use none of them doesnt work for me that means it shows resource not found error. please guide me.
Try install the Route Debugger nuget package it may help you see why things aren't matching
To say that routing is not my strong suite is an understatement, so please bear with me.
I have this link in one of my pages and I need my MVC route to pick this up and map it to the
"MyArea" area
"MessagesController" controller
"Inbox" method
http://localhost/MyArea/messages/list.asp?pjid=&box=in&sf=mesibox
What I've come up with so far does not cut it.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("favicon.ico");
routes.MapRoute(
"Messages",
"messages/list.asp",
new
{
controller = "Messages",
action = "Inbox"
});
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new
{
controller = "Account",
action = "LogOn",
id = UrlParameter.Optional
}
);
Any help would be appreciated.
Thank you,
Stephen
UPDATE:
Darin answered a similar question here How to route legacy QueryString parameters in ASP.Net MVC 3? so that takes care of the query string parameters. Now to figure out the main segments.
You seem to have defined an area, so it is inside the routing configuration of this area that you may try adding that route and not inside global.asax:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"MyArea_default",
"MyArea/messages/list.asp",
new { controller = "messages", action = "inbox" }
);
context.MapRoute(
"MyArea_default",
"MyArea/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
Perhaps I do not understand correctly how MVC Areas work, but this has got me a little confused.
Add an Area called "MyArea" using right-click "Add Area" in Visual Studio on the MVC3 project
Create a controller for MyArea: "AnArea" with matching view in the MyArea area.
Add "controller = "AnArea" to context.MapRoute's defaults parameter in MyAreaAreaRegistration.RegisterArea method.
So at this point if you start the application and navigate to /MyArea/ it should load the AnArea controller with it's matching view. If you navigate to /MyArea/AnArea, it will show the same result.
But, if you navigate to /AnArea/, the controller is still found and the following error message is displayed:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/anarea/Index.aspx
~/Views/anarea/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/anarea/Index.cshtml
~/Views/anarea/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
Is this the correct behaviour? I would have thought an area's controller could only be accessed via it's own area and not globally.
Whenever I create an project with areas, I change my Default route as follows:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // defaults
null, // constraints
new string[] { "MyApplication.Controllers" } // namespaces
);
The final parameter limits the default route to the controllers in the MyApplication.Controllers namespace. This insures that the Default route is limited to actions outside of any areas.
UPDATE
After a deep dive into the code, I discovered where the issue arises, and have a solution. Change your Default route to the following:
routes.Add(
"Default",
new Route("{controller}/{action}/{id}",
new RouteValueDictionary(
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
),
null,
new RouteValueDictionary(
new {
Namespaces = new string[] { "MyApplication.Controllers" },
UseNamespaceFallback = false
}
),
new MvcRouteHandler()
)
);
The key is in adding the UseNamespaceFallback token. This will prevent the Default route from looking into any other namespaces.
This is unexpected behavior, and it was a problem I was unaware of which affects a project I am working on. I will list it as an issue at aspnet.codeplex.com. I would not call this a bug, but the behavior definitely appears to breach the convetions for MVC routing.
You have to apply a namespace restriction in both area and general route.
In global.asax.cs you should edit RegisterRoutes method just like this
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "MyProject.Controllers" }
);
}
That will restrict "//" only to the namespace "MyProject.Controllers"
But also you´ll have to apply the namespace restriction to the Area to restrict "//" only to the namespace "MyProject.Areas.MyArea.Controllers"
For that you´ll have to edit "RegisterArea" method of "MyAreaAreaRegistration.cs" like below ("MyAreaRegistration.cs" is located at "/MyProject/Areas/MyArea" folder ) :
//Some default code stuff
...
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"MyArea_default",
"MyArea/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new string[] { "MyProject.Areas.MyArea.Controllers" }
);
}
Hope it helps!!
You seem to be navigating to /AnArea whereas your area is called MyArea so you should navigate to /MyArea/. Here's how the area route registration looks like:
context.MapRoute(
"MyArea_default",
"MyArea/{controller}/{action}/{id}",
new { controller = "AnArea", action = "Index", id = UrlParameter.Optional }
);
AnArea is the name of the controller, not the area. If you want to navigate to some controller of this area you should always prefix your url with MyArea which is the name of the area.
I am in the process of debugging a routing issue on my MVC 3 application and I am using Phil Hacks routing debugger.
I cannot seem to work out where the route highlighted in yellow below is originating. Each time I run my application with the following request
http://www.mywebsite.com/auth/login?ReturnUrl=/
this route comes first and then gives me a 404 error as I do not have an index action. As you can see I have set my default routes to use the Login action method but still this route persists.
I have the following route configurations:
AuthAreaRegistration
public class AuthAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Auth";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"login",
"auth/login/{*returnPath}",
new { controller = "Auth", action = "LogIn", id = UrlParameter.Optional }
);
context.MapRoute(
"Auth_default",
"Auth/{controller}/{action}/{id}",
new { controller = "Auth", action = "LogIn", id = "" }
);
}
}
Global.asax (Using T4 MVC Templates)
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Home",
"{controller}/{action}/{id}",
MVC.Home.Index(), new { id = UrlParameter.Optional },
new string[] { "MyNamespace.WebUI.Controllers" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
MVC.Home.Index(), new { id = UrlParameter.Optional },
new string[] { "MyNamespace.WebUI.Controllers" }
);
}
I don’t like to answer my own question but after a day of trying to solve this problem I thought I would post the answer in case anyone else has the same issue.
It turns out that my application was holding onto old routes and populating them into my route collection. I deleted all the files in my bin folder and rebuilt my solution and everything worked as it should.
I have answered this question in a little more detail here:
Does ASP.NET MVC create default routes for areas
I think the problem is in the fact that you have an area called Auth and a controller called Auth outside of the areas.
MVC will try to match your url against Auth area first but you actually want it to hit your auth controller outside an area.
The best way to solve it imho is to avoid a ambiguous names of controllers/areas.