问题描述
我什至不确定我是否知道正确表达这一点的正确术语.我是 MVC 的新手,继承了一个部分完成的项目.
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.
该项目使用区域,我对 MVC 和路由的了解不够,无法解决以下问题:
The project uses Areas, I don't understand MVC and routing enough to resolve the following:
我有一个站点,例如www.b2c.com",并且有一个区域OnlineOrder"控制器Home"并查看Index".我希望访问者在输入网址时默认转到http://www.b2c.com" --> 在线订单/首页/索引网址http://www.b2c.com/OnlineOrder/Home/Index"工作正常.
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/Indexthe url "http://www.b2c.com/OnlineOrder/Home/Index" works fine.
我的 RouteConfig 文件包含:
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");
}
}
不知道该怎么做.
推荐答案
在您的默认路由上方添加到您的 RouteConfig.cs 文件或删除默认路由.
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();
}
谢谢
这篇关于MVC5 路由在输入站点的根 url 时转到特定区域视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!