Reputation: 42514
I have to create a site structure like this
/demo/
/demo/admin/create
/demo/admin/edit/id
I can create a DemoController which will contains admin. But how to show create/edit pages? The create/edit pages can be accessible only after user is logged in. Where to put create/edit pages?
Upvotes: 2
Views: 7962
Reputation: 2431
Pretty old question, Google landed me here right now.
There is also another way to reach the goal: the Route
and RoutePrefix
attribute.
Just a small chunk of code for reference.
[RoutePrefix("demo")]
public class DemoController : Controller
{
[Route("")]
public ActionResult Index() { } // default route: /demo
[Route("admin/create")]
public ActionResult Create() { } // /demo/admin/create
[Route("admin/edit/{id}")]
public ActionResult Edit(int id) { } // /demo/admin/edit/5
}
For this to work, the attribute routing must be enabled. In most case is enough to add:
routes.MapMvcAttributeRoutes();
in RouteConfig.cs.
Example:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Dashboard", action = "Index", id = UrlParameter.Optional }
);
}
Upvotes: 1
Reputation: 1767
i agree an area should do the trick or you can add a custom route that points the the controller if you want to lock down the whole section as an admin only section i think areas would be the way to go on this one
Upvotes: 1
Reputation: 93474
All you need to do is create a route for /demo/admin, then assign that route to a new controller called DemoAdminController. To make this only accessible to logged in users, you use the Windows Forms authentication system. A sample is provided with the default application generated by MVC.
Upvotes: 1
Reputation: 414
If you are certain that you should implement strictly that URL strusture, then maybe "areas" solution would fit you (though not sure, just had a brief view). But I think, that for a small project you could simply make:
As for the authorization, you should look into ASP.NET Web Application Security and User authentication and authorisation in ASP.NET MVC
Upvotes: 3
Reputation: 3974
I think you are looking to use Areas. See docs here: http://msdn.microsoft.com/en-us/library/ee671793.aspx
Upvotes: 0