So far I can't make this work and I need some assistance please. I have a basic MVC 5 site, and I have added an area called Administration. For the life of me, I can't figure out how to set a default controller/action for an area, properly.
In my site, I have an area named Admin, a controller named Admin (with Index method and view), and this is the area registration:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "Admin_base",
url: "Admin",
defaults: new { area = "Admin", controller = "Admin", action = "Index", id = UrlParameter.Optional }
);
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
The first MapRoute allows me to browse to http://myapplication/Admin and it displays a view that I set (Admin/Index) and the URL stays http://myapplication/Admin (which is what I want). Now, by adding this, it breaks any further routing to a controller. So when I attempt to navigate to the Menu controller in the Admin area, it fails.
Is there a correct way to do this?
i.e. I need to make http://myapplication/Admin/Menu/Create route properly, but also need to retain the default Controller/Action for the area.
You should just be able to combine them into one:
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
defaults: new { area = "Admin",
controller = "Admin",
action = "Index",
id = UrlParameter.Optional }
);
By assigning defaults, you should be able to call /Admin/ and the rest of the parameters are set to the defaults.
If you are using the same controller name for Area and default one(For eg: HomeController). Use the namespaces for that
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new {
area = "Admin",
controller = "Home",
action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "{{Namespace}}.Areas.Admin.Controllers" }
);
for default one
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index",
id = UrlParameter.Optional },
namespaces: new[] { "{{Namespace}}.Controllers" }
);
Related
I have an MVC 5 site with this structure:
example.com
example.com/area1 (index for whole section)
example.com/area2/item5 (specific item for section)
For SEO reasons I'd like to change every URL to this:
example.com/keyword (redirected from example.com)
example.com/keyword/area1
example.com/keyword/area2/item5
The keyword is fixed and always the same, though at some point in the future there may be a site with the same structure but different content with a different keyword in. That is probably at least a few months off though.
What is the quickest / easiest way to implement the above. Being able to get the name of keyword would be an advantage later though isn't essential right now.
I could use attribute routing, though I'd have to update A LOT of ActionMethods to do this - and most currently don't even use attribute routing.
thx.
Update
I've tried adding the below to RouteConfig.cs, but none of the Url's work for some reason:
routes.MapRoute(
name: "Default",
url: "keyword/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
string keyword = "keyword";
// keyword route - it will be used as a route of the first priority
routes.MapRoute(
name: "Defaultkeyword",
url: "{keyword}/{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional,
keyword = keyword
});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
also you need to add this Defaultkeyword route with few changes into your Area1AreaRegistration.cs file
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Area1_defaultKeyword",
"{Keyword}/Area1/{controller}/{action}/{id}",
new { Keyword = "Keyword", controller = "HomeArea1", action = "Index", id = UrlParameter.Optional }
);
context.MapRoute(
"Area1_default",
"Area1/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
I have two different areas in ASP.NET MVC Web Application.
1. Admin
2. Site
I want to have the default route of my application to the Site Area
i.e www.mydomain.com/Site/Home
For that I have configured the default route as follows.
But this configuration does not work for me.
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 },
namespaces: new string[] { "MyApp.Areas.Site.Controllers" }
);
}
The Site Area Route is as follows.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "Site_default",
url: "Site/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);
}
The Admin Area Route is as follows.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
defaults: new { controller="Login",action = "Login", id = UrlParameter.Optional}
);
}
Your help will be much appreciated
I think you've wrong controller namespaces.
Your main route definition should be:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "MyApp.Controllers" }
);
And your areas route like admin area registration should be:
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new { controller = "Home" },
new[] { "MyApp.Areas.Admin.Controllers" }
);
Following steps to do:
Create a Project: Project Type: Framework 4.6.1 > WebApi and MVC Web
Project Layout will have [Areas Folder]
Inside [Areas folder] you'll find folder [HelpPage Folder]
Right-click >> [Areas folder] Select Area > "Give it a name: Administration"
Visual Studio Register everything for you.
Areas
Administration
Controllers
Models
Views
AdministrationAreaRegistration.cs
Filestructure example
Create a Controller > read write : Give it a name [Admin] and [View] Indexenter image description here
Then in your layout add link : Under the normal Home link
#Html.ActionLink("Home", "Index", "Home", new { area = "" }, null)
#Html.ActionLink("Admin", "Index", "Admin", new { area = "Administration" }, null)
Click on the link and see your Administration area
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 am having trouble trying to get a route working in an area I have.
My area is called ABC and I have a controller called Home within the area. I am able to hit the breakpoint with Home/Index if I browse "http://localhost:8000/abc" however when I try hit another action called details like "http://localhost:8000/ABC/details" I get a 404.
I have tried
context.MapRoute(
"details",
"ABC/Home/{action}/{id}",
new { action = "details", id = UrlParameter.Optional },
constraints: null,
namespaces: new[] { "WebApplication.Areas.ABC.Controllers" }
);
context.MapRoute(
"ABC_Home",
"ABC/{controller}/{action}/{id}",
new { controller = "home",action="Index", id = UrlParameter.Optional },
constraints: null,
namespaces: new[] { "WebApplication.Areas.ABC.Controllers" }
);
This allows me to hit the action if I use "http://localhost:8000/ABC/Home/Details"
context.MapRoute(
"details",
"Home/Home/{action}/{id}",
new {controller="home", action = "details", id = UrlParameter.Optional },
constraints: null,
namespaces: new[] { "WebApplication.Areas.ABC.Controllers" }
);
Ideally I don't want to use home within the url if possible. What am I doing wrong?
Any help would be awesome!
I think you would just need a single route for this. Don't include the controller in the route, since it seems to be implied by starting with /ABC; just assign the controller as a default value:
context.MapRoute(
"ABC_Home",
"ABC/{action}/{id}",
new { controller = "home", action="Index", id = UrlParameter.Optional },
constraints: null,
namespaces: new[] { "WebApplication.Areas.ABC.Controllers" }
}
Per your requirements, this will route /abc to /home/index, and will route /abc/details to /home/details.
Then, if you need to access other controllers you can add another rule for that, something like the default one:
context.MapRoute(
"Default_Route",
"{controller}/{action}/{id}",
new { id = UrlParameter.Optional }
}
I don't think you can default a controller with a variable action name, otherwise there's no way to tell from the routes that it is an action or controller and which route to match. I think you could hard-code the action though:
Context.MapRoute(
"ABC_Home_Details",
"ABC/Details/{id}",
new { controller = "home", action="details", id = UrlParameter.Optionsl },
constraints: null,
namespaces: new [] { "WebApplication.Areas.ABC.Controllers" }
);