0

I have area and in that area I have Home/Index that contains Authorize attribute.
What is the proper way to make this action default for my application.
So when user type:
mysite.com to open Home/Index
Code in areaRegistration.cs

context.MapRoute(
                "CityPage_default",
                "CityPage/{controller}/{action}/{id}",
                new { controller="Home", action = "Index", id = UrlParameter.Optional }
            );
1110
  • 7,829
  • 55
  • 176
  • 334

1 Answers1

1

You can't do this with areas, because the only way ASP.NET MVC could know that a request belongs to a given area is by using a prefix in the url. So the closest you could get is http://mysite.com/myarea. As far as the Home and Index are concerned, simply use default values in your route registration. This way you don't need to specify them in your url.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Should this route setup be in global.asax? – 1110 Feb 11 '12 at 14:18
  • @1110, in your `~/Areas/AreaName/AreaNameAreaRegistration.cs`. – Darin Dimitrov Feb 11 '12 at 14:20
  • I added code from my areaRegistration.cs but this doesn't work – 1110 Feb 11 '12 at 14:22
  • Where is the Home controller defined? Is it in the area? In which namespace? I tested this and it worked fine. – Darin Dimitrov Feb 11 '12 at 14:28
  • Home controller is in the CityPage area. Namespace is Project.WebUI.Areas.CityPage.Controllers. I don't know is it important I also have a few other areas (Security for login,register etc. and Admin for site administration) – 1110 Feb 11 '12 at 14:32
  • Do you have other HomeControllers in other parts of your application? – Darin Dimitrov Feb 11 '12 at 14:33
  • @1110, sorry I cannot reproduce your problem. Steps: 1. create a new ASP.NET MVC 3 site 2. Remove `HomeController.cs` from `~/Controllers` 3. Add a new Area called `CityPage` 4. Add a HomeController in this area with a single Index action which `return Content("foo bar");` 5. In `~/Areas/CityPage/CityPageAreaRegistration.cs`, add `controller = "Home"` to the list of constraints in the route definition 6. Run the application and navigate to `http://localhost:xxxx/citypage` 7. The Index action of HomeController is executed and the result is presented on the screen. – Darin Dimitrov Feb 11 '12 at 15:14
  • So if you need help please provide a step by step guide (as I did) in order for us to be able to reproduce your issue. Narrow it down of course to a very simple application (as I did). – Darin Dimitrov Feb 11 '12 at 15:15
  • I did all the same. 1)Create a new empty project. 2)Remove all from ~/Controllers 3) Add CityPage area 4) Add Home controller 5) Add index View 6) Added controller="Home" to CityPageAreaRegistration.cs 7) run app and url is http://localhost:xxxx/ 8) Error on page is "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" 9) If I manually type http://localhost:xxxx/citypage it works but I need to do this automatically. – 1110 Feb 11 '12 at 15:27
  • @1110, oh, I get it. Didn't you read my answer? Go ahead and read it once again. The only way that ASP.NET MVC knows that a request to some area is being made is by using the prefix in the url. When you type `http://localhost:xxxx` without any prefix, ASP.NET MVC cannot possibly know that you are referring to the `CityPage` area. – Darin Dimitrov Feb 11 '12 at 15:30
  • So you wan't to say that when user type mysite.com there is no way to navigate him automatically to mysite.com/citypage? – 1110 Feb 11 '12 at 15:35
  • @1110, you could use a redirect. Actually it's possible but you might need to write a custom view engine: http://stackoverflow.com/a/2172349/29407 – Darin Dimitrov Feb 11 '12 at 15:36
  • Can u suggest me what is good place to redirect user to area when he visit site? What is first called when app is started? – 1110 Feb 11 '12 at 17:00
  • @1110, one possibility is to put a default.aspx file in the root of your site that will perform the redirect. – Darin Dimitrov Feb 11 '12 at 17:01
  • I closed question I decided to change a project structure a "little" I will one main area store in "~/..." and store other parts in areas. Happily this in still beginning :) – 1110 Feb 11 '12 at 19:36