1.启用路由前的准备工作

1.Global.asax.cs中注册路由

public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
//===============注册区域===============
AreaRegistration.RegisterAllAreas();
//===========注册路由======================还可以注册全局过滤器...
RouteConfig.RegisterRoutes(RouteTable.Routes);
}

2.添加域

项目名右键单击--->添加--->区域
再对应的控制器目录下写一个控制器并添加视图

3

其中adminAreaRegistration.cs与hvAreaRegistration.cs为子路由,,用默认值即可;

此时,http://localhost:33777/admin/home/index与http://localhost:33777/hv/home/index可分别访问对应的域的控制器。

using System.Web.Mvc;
namespace MyMvcAreasDemo.Areas.admin
{
public class adminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"admin_default",
"admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
}
using System.Web.Mvc;
namespace MyMvcAreasDemo.Areas.hv
{
public class hvAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "hv";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"hv_default",
"hv/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
}

3.App_Start\RouteConfig.cs配置路由


using System.Web.Mvc;
using System.Web.Routing;
namespace MyMvcAreasDemo
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "MyMvcAreasDemo.Areas.admin.Controllers" }
).DataTokens.Add("area", "admin");
}
}
}

上文注释:

最新文章

  1. PPT设计宝典!十招教你做出拿得出手的PPT
  2. js中return,this,arguments,currentStyle和getComputedStyle小析
  3. 使用GIT进行源码管理——GIT托管服务
  4. 理解 Python 中的 *args 和 **kwargs
  5. 分布式缓存技术redis学习(三)——redis高级应用(主从、事务与锁、持久化)
  6. 9款基于HTML5/SVG/Canvas的折线图表应用
  7. oracle字段类型
  8. Android UI开发第三十四篇——SlidingPaneLayout
  9. 【POJ2094】【差分序列】Angry Teacher
  10. 人工神经网络简介和单层网络实现AND运算--AForge.NET框架的使用(五)
  11. IOS网络开发实战(一)
  12. 关于ThinkPHP下使用Uploadify插件 仅有火狐提示HTTP Error (302)错误的解决办法
  13. 初识Java多线程编程
  14. 跨域1-CORS跨域
  15. 通过命令“du–sk”, “du–Ask” 的区别,谈谈如何在有保护的文件系统中查看文件或文件夹的大小
  16. layer层、modal模拟窗 单独测试页面
  17. 20170803 Airflow自带的API进行GET 和POST动作部分内容
  18. 学习大数据基础框架hadoop需要什么基础
  19. Windows 8 禁用强制驱动签名
  20. 大数据分析界的“神兽”Apache Kylin有多牛?【转】

热门文章

  1. linux记录-安装zabbix监控系统
  2. 报错:java.lang.ClassNotFoundException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
  3. Spring Cloud Config 分布式配置管理 5.3
  4. 【Leetcode_easy】849. Maximize Distance to Closest Person
  5. API SendMessage 带参数
  6. JavaScript控制浏览器全屏及各种浏览器全屏模式的方法、属性和事件
  7. 与TypeScript的一场美丽邂逅
  8. 最小二乘求解常数k使得kx=y(x,y为列向量)
  9. Ubuntu中打开Qt creator,提示无法覆盖文件 /home/username/.config/Nokia/qtversion.xml : Permission denied
  10. [转帖]nginx基础整理