Thursday, January 22, 2015

Area Registration in MVC

Main RouteConfig.cs  
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 },
                namespaces: new[] { "MvcApplication1.Controllers" }
                //provide fully qualified namespace of the controller class
               );
        }
    }
 
 
2. AdminAreaRegistration.cs
namespace MvcApplication1.Areas.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", id = UrlParameter.Optional }
       // CTM to also specify controller name in the respective AreaRegistration.cs files.
                
               );
        }
    }
} 

3.
<h2>Admin Home Page</h2>
<table>
    <tr>
        <td>Admin Home</td>
        <td>@Html.ActionLink("Admin Home""Index""Home"new { area = "Admin" } , null)</td>
    </tr>
    <tr>
        <td>Employee Home</td>
        <td>@Html.ActionLink("Employee Home""Index""Home"new { area = "Employee" }, null)</td>
    </tr>
    <tr>
        <td>Site Home</td>
        <td>@Html.ActionLink("Site Home""Index""Home",  new { area = "" }, null)</td>
    </tr>
</table>

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More