Sephiroth
Sephiroth

Reputation: 126

Asp.NET MVC3 - Access Area's Controller Without Area in URL

I created a web application with ASP.NET MVC3 Framework. In project,I add new area called Administrator with HomeController and Index action.

It work with http://localhost:2813/Administrator/Home/Index

But when I try access with URL http://localhost:2813/Home/Index

I get a error message :

Server Error in '/' Application.

The view 'Index' or its master was not found or no view engine supports the searched > > > locations. The following locations were searched: ~/Views/Home/Index.aspx ~/Views/Home/Index.ascx ~/Views/Shared/Index.aspx ~/Views/Shared/Index.ascx ~/Views/Home/Index.cshtml ~/Views/Home/Index.vbhtml ~/Views/Shared/Index.cshtml ~/Views/Shared/Index.vbhtml

Description: An unhandled exception occurred during the execution of the current web > > > request. Please review the stack trace for more information about the error and where it > originated in the code.

I alse add namespace in maproute but it still not work. Anyone has idea? Thanks.

Update question:

This is MapRoute in Global.ascx routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Default", action = "Index", id = UrlParameter.Optional }, new[] { "SampleWebsite.Controllers" } );

and this is AdministratorAreaRegistration.ascx context.MapRoute( "Administrator", "Administrator/{controller}/{action}/{id}", new { area = "Administrator", controller = "Dashboard", action = "Index", id = UrlParameter.Optional }, new[] { "SampleWebsite.Areas.Administrator.Controllers" } );

EDIT
Problem was resolved if I move MapRoute from Global to AdministratorAreaRegistration (I can not understand and explain its)
context.MapRoute( "Administrator", "Administrator/{controller}/{action}/{id}", new { area = "Administrator", controller = "Dashboard", action = "Index", id = UrlParameter.Optional }, new[] { "SampleWebsite.Areas.Administrator.Controllers" } );
context.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { area = "", controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults namespaces: new[] { "SampleWebsite.Controllers" } );

Now, all request to localhost/Home/Index will be response 404 not found. Only request with exactly area name (Administrator) allowed.

Thanks for you help!

Upvotes: 1

Views: 2704

Answers (3)

Christophe Argento
Christophe Argento

Reputation: 193

Thanks for adding the routes. If what you want is using your admin area when there is no area specified in url, you can try replacing your route in the Global.asax with:

routes.MapRoute( "Default", "{controller}/{action}/{id}", new { area = "Administrator", controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "SampleWebsite.Areas.Administrator.Controllers" } );

Upvotes: 1

Pol
Pol

Reputation: 5134

If you need start page inside area, consider this in HomeController of default area (/Controllers/HomeController.cs):

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return RedirectToAction("Index", "Home",
            new { area = "Administrator" });
    }
}

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039160

When you use the url /Home/Index you are not going through the area. An area is defined in your routing with an Administrator prefix and it is the presence of this prefix in the url that allows the routing engine to recognize that you are inside the area.

There are some possible workarounds to have a default area.

Upvotes: 2

Related Questions