ASP.NET MVC Areas: How to hide “Area” name in URL?

后端 未结 2 1331
星月不相逢
星月不相逢 2020-12-28 18:45

When running the MVC 2 Areas example that has a Blog Area and Blog Controller the URL looks like this:

http://localhost:50526/Blog/Blog/ShowRecent in the format:

2条回答
  •  独厮守ぢ
    2020-12-28 19:03

    Just to answer your original question, in case someone else is reading this:

    Do not name your [default] controller blog. This is why you get blog/blog {area/controller}. You can either give it a completely different name: i.e., blog/view, blog/posts, blog/recent, etc. or, a default like home. in this case, if you also have home in your out-of-area controllers, you'll want to namespace your default controller:

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

    This will ensure that "/" and "/blog" go to the appropriate "home" controller. If you search for the duplicate home controller error you'll find more on this.

提交回复
热议问题