MVC5 Routing Go to specific Areas view when root url of site is entered

青春壹個敷衍的年華 提交于 2019-12-11 11:05:17

问题


I'm not even sure I know the right terminology to articulate this correctly. I'm new to MVC and have inherited a partially complete project.

The project uses Areas, I don't understand MVC and routing enough to resolve the following:

I have a site eg "www.b2c.com" and have an area "OnlineOrder" controller "Home" and view "Index". I want visitors to go to by default when they enter the url "http://www.b2c.com" --> OnlineOrder/Home/Index the url "http://www.b2c.com/OnlineOrder/Home/Index" works fine.

My RouteConfig file contains:

   public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
            "Default_Home", // Route name
            "",        // URL with parameters
            new { controller = "Home", action = "Index" }// Parameter defaults

      );

And I have a area registration file in the root folder of the area:

public class OnlineOrderAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "OnlineOrder";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(

                        name: "Info",
              url: "OnlineOrder/{controller}/Info/{action}",
              defaults: new { controller = "Home", action = "Index" }
          );
            context.MapRoute(
                name: "Default",
                url: "OnlineOrder/{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            ).DataTokens.Add("areas", "OnlineOrder");


        }
    }

Not sure what to do.


回答1:


Add to your RouteConfig.cs file above your Default route or remove Default route.

routes.MapRoute(
    name: "Area",
    url: "",
    defaults: new { area = "OnlineOrder", controller = "Home", action = "Index", id = UrlParameter.Optional }
);

This will confirm your area is 'OnlineOrder'

public ActionResult Index()
{
    ViewBag.Message = RouteData.Values["area"];

    return View();
}

Thanks




回答2:


you have two possible Option

  1. you can change default route
  2. Perform redirect operation in your home controllers' index action


来源:https://stackoverflow.com/questions/30322218/mvc5-routing-go-to-specific-areas-view-when-root-url-of-site-is-entered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!