Routing in ASP.NET MVC with multiple areas - asp.net-mvc

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

Related

MVC Areas - Controller does not return view

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 .

MVC5 Custom Route to Remove Controller Name from Area

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" }
);

Custom Routing in mvc4

I am using mvc4 framework and .net framwork 4.5. I need a url like this:
www.examples.com/name (note: 'name' will be change dynamically)
which routes to the same page.
I have tried like this but getting error
My action method is like this:
public ActionResult Userlist(string status)
{
return View();
}
Route Config
routes.MapRoute(
"user",
"{status}",
new { controller = "Home", action = "Userlist" }
);
How can I create a route syntax and redirect this to a controller?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Add this route config
routes.MapRoute(
name: "Default_Userlist",
url: "{status}",
defaults: new { controller = "Home", action = "Userlist" },
namespaces: new[] { "MyMvcProject.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyMvcProject.Controllers" }
);
}
Don't forget to replace the "MyMvcProject" with the name of your project.
Issue
This is a bad practice because the value of the status could easily cause conflicts with your action methods.

Two languages on site asp.net mvc 4

I study a lesson which describes how to create a site with two or more languages,
and the first step in the lesson - I need to add the following code in my project
context.MapRoute(
name: "lang",
url: "{lang}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints : new { lang = #"ru|en" },
namespaces: new[] { "LessonProject.Areas.Default.Controllers" }
);
context.MapRoute(
name : "default",
url : "{controller}/{action}/{id}",
defaults : new { controller = "Home", action = "Index", id = UrlParameter.Optional, lang = "ru" },
namespaces : new [] { "LessonProject.Areas.Default.Controllers" }
);
in file DefaultAreaRegistration (/Areas/Default/DefaultAreaRegistration.cs)
but I dont have this file in my project.
I dont understand I need to create a new folder Areas and a new file DefaultAreaRegistration.cs or I need to change RouteConfig.cs file which contains
public class RouteConfig
{
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 in Global.asax there is the following code
AreaRegistration.RegisterAllAreas();
What's part I need to change?
you need to add this code to DefaultAreaRegistration.cs if this area was created before.
It means that the multiculture route cannot be created automatically when area creating. So if you dont have any Areas in your solution you just need the register route in RouteConfig.cs. its alredy done.
And u dont need any Areas for multyculture functionality.

Mvc area routing?

Area folders look like :
Areas
Admin
Controllers
UserController
BranchController
AdminHomeController
Project directories look like :
Controller
UserController
GetAllUsers
area route registration
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new { controller = "Branch|AdminHome|User" }
);
}
project route registration
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.Admin.Controllers" });
}
When I route like this: http://mydomain.com/User/GetAllUsers I get resource not found error (404). I get this error after adding UserController to Area.
How can I fix this error?
Thanks...
You've messed up your 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 Admin area route registration should be:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new { controller = "Branch|AdminHome|User" },
new[] { "MyApp.Areas.Admin.Controllers" }
);
}
Notice how the correct namespaces should be used.
An up to date solution for ASP.NET Core MVC.
[Area("Products")]
public class HomeController : Controller
Source: https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/areas

Resources