问题
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
- you can change default route
- 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