1

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!

Sephiroth
  • 116
  • 2
  • 7

3 Answers3

2

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.

Community
  • 1
  • 1
Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
  • I just read your recommend link and think that may be work for my problem.Thanks you! – Sephiroth Jun 11 '12 at 07:42
  • I juss had free time to try this solution. It is great work but I think it far from ideal. My problem is If i try access with `localhost:2813/Home/Index`, it should redirect to 404 not found instead of go to HomeController of Admin Area. – Sephiroth Jun 11 '12 at 13:46
1

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" });
    }
}
Pol
  • 4,969
  • 3
  • 30
  • 50
1

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" } );
  • Thanks for reply. If I access to `http://localhost/Home/Index`, this route will redirect to `http://localhost/Administrator/Home/Index`, is it right? But in this, I want `http://localhost/Home/Index` redirect to error 404 : page not found. This controller is only owned by Administrator. Default controllers of project does not have HomeController. – Sephiroth Jun 11 '12 at 15:37
  • 1
    No there won't be a redirect. The action from the Admin area will be called. If you try to handle access rights, this is not the good way. Areas are not meant for this. Try looking at the authorization filters if this is wat you want. – Christophe Argento Jun 11 '12 at 18:40