Are namespaces equivalent in ASP.NET MVC?

In rails, I could create a namespace to encapsulate the views inside the given name (or URL prefix)

What I want to do is create a namespace (or Area I believe?), Which should encapsulate all the admin controllers inside the given name.

For example, I want to create an Admin namespace, where whenever I go to www.myapp.com/admin/ I get the controller administrator using the index method and every time I go to www.myapp. com / admin / products, it must invoke the product controller using the index method and so on, because I also want to restrict these controllers to the person who needs to log in, as in.

URL and routing wise, how can I accomplish the ones mentioned earlier?

+3
source share
2 answers

Infact function called Areas in asp.net mvc.

You right-click your project in Visual Studio and click "Add Area".

You will now have a folder with folders for Views, Controllers, and Shared Folder. A route is also added to the project.

Snag: There is a case where this can cause a problem if you have HomeControllerinside one of your areas, as this will go against the route HomeControllerfor the website root. Stephen Sanderson corrected this in his book:

Change your default route:

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

See MDSN Articles .

Video on Asp.net sites.

Good article by Stephen Sanderson:

+4

MvcCodeRouting , URL-, , .

0

All Articles