0

I created an Area in MVC then I created a simple controller and I can't view the Area or any page within that part of my site does anyone know why and how to fix this problem. I created a project a while back with an Area and had no problems, I don't know what has changed or if I had to change something to make it work can somebody please help me out.

Somewhere it's not getting registered.

namespace RedPlanet.Areas.admin.Controllers
{
    public class CategoryController : Controller
    {
    }
}

Area:

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 { action = "Index", id = UrlParameter.Optional }
        );
    }
}

Global ASAX:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();

        System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<DataContext>());
    }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
ONYX
  • 5,679
  • 15
  • 83
  • 146

1 Answers1

0

You need to specify the namespace when registering the route for your admin area and Main Route in you Application.

Main Route :

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        new string[] { "RedPlanet.Controllers" }// specify the namespace
);

In your \Areas\admin\adminAreaRegistration.cs file, you need to modify the RegisterArea() method as follows:

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "admin_default",
        "admin/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        new[] { "RedPlanet.Areas.admin.Controllers" }// specify the namespace
    );
}

Update :
if you are using Guid you should change parameter type of your action method to Guid :

public ActionResult Details(Guid guid)
{
    //code
    return View(model); 
}

then create new route for that ;

routes.MapRoute("FooRouteName",
        "Controller/Action/{guidParam}",
         new { controller = "Category", action = "Index" },
         new { guidParam = @"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$" }
    );
Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110
  • I copy and pasted you're code in and add Category to the second set of code and I'm still getting nothing – ONYX Oct 03 '13 at 04:19
  • It's a blank error page in red with asp.net resource page. It' says resource not found – ONYX Oct 03 '13 at 04:25
  • I've recreated it a few times and I'm always getting the same problem. Also when I create an ActionLink like this @Html.ActionLink("Admin Area", "Index", "admin/Category") I get no link – ONYX Oct 03 '13 at 04:26
  • I copied you're code and It's still the same for the Area getting a resource not found page error 404 but the first code works but I don't understand what is wrong – ONYX Oct 03 '13 at 04:31
  • where is your action method when you're calling `{yoursite}/admin/Category` it should calls Index action method inside CategoryController, Do you have it? – Sirwan Afifi Oct 03 '13 at 04:36
  • Yeah it's in there. I created it with that wizard which generates the code for you when you select a DataContext and Model so there is Index Create Edit and Delete, I haven't had this problem before so I have know idea as to why this is hapeening – ONYX Oct 03 '13 at 04:38
  • Ok so I created just a normal Controller without an Area and I'm getting the same problem as well. I'm using Guid for my Id's and setting them to Guid? as a paramater but I can't see why that would be affecting the route – ONYX Oct 03 '13 at 04:47
  • if you're using Guid you should change Parameter type of your action method to Guid. and then write another route for that, look at here : http://stackoverflow.com/questions/7563675/differentiate-guid-and-string-parameters-in-mvc-3 – Sirwan Afifi Oct 03 '13 at 04:51
  • I created a new project and didn't use Guid and I'm still getting the same problem – ONYX Oct 03 '13 at 05:09
  • please put your controller code in your question and that error you're getting. – Sirwan Afifi Oct 03 '13 at 05:14