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");
}
}
}

上文注释:

MVC-区域(Area)的更多相关文章

  1. MVC区域area

    1.当项目业务比较庞大,可以通过区域来分拆. 2.添加区域时,默认会生成一下文件. 3.Application_Start()必需含有AreaRegistration.RegisterAllAreas ...

  2. Asp.Net MVC part4 异步、校验、区域Area

    异步方式1:使用jquery的异步函数方式2:使用MVC的AjaxHelper行为的返回值设置:JsonResult对象,使用Json方法接收一个对象,在内部会完成对象的js序列化,向输出流中输出js ...

  3. MVC 添加Area

    在MVC项目中经常会使用到Area来分开不同的模块让项目结构更加的清晰. 步骤如下: 项目 –> 添加 -> 区域 ( Area ) 输入 Admin 添加成功后 Area包含: 创建一个 ...

  4. ASP.NET MVC中Area的另一种用法

    ASP.NET MVC中Area的另一种用法 [摘要]本文只是为一行代码而分享 context.MapRoute("API", "api/{controller}/{ac ...

  5. 将子域名请求路由到MVC区域

    写了个扩展,分享给需要的朋友. 0x01 使用方法 在mvc区域中的{xxxx}AreaRegistration.cs文件中,如ProjectsAreaRegistration.cs <pre& ...

  6. MVC 区域路

    VS2013和VS2015中MVC 区域路由匹配顺序相反   创建测试工程 分别在vs2013和vs2015中创建mvc项目,并创建First.Second.Three三个Area,每个Area下面创 ...

  7. MVC教程:MVC区域路由

    一.区域路由 为了管理网站中大量的文件,在ASP.NET MVC 2.0版本中引入了一个新概念:区域(Area). 有了区域以后,可以让我们的项目不至于太复杂而导致管理混乱.每个模块的页面都放入相应的 ...

  8. Blazor使用区域(Area)

    在MVC中,我们经常使用区域(Area)来区分各个模块,比如后台我们可以写一个Admin的Area. 到了Blazor时代,已经不推荐这么做了,现在推荐的做法是通过Url来区分,比如Admin可以配置 ...

  9. MVC区域 视图必须派生自 WebViewPage 或 WebViewPage<TModel>

    http://blog.csdn.net/iack_ji/article/details/16965885 今天在学习 mvc区域时,将区域控制器类 外迁到其他的程序集的练习中出现了"视图必 ...

  10. MVC 插件化框架支持原生MVC的Area和路由特性

    .NET MVC 插件化框架支持原生MVC的Area和路由特性 前面开放的源码只是简单的Plugin的实现,支持了插件的热插拔,最近晚上偶然想到,原生的MVC提供Areas和RouteAtrribut ...

随机推荐

  1. mat文件读写

    一起来学演化计算-mat文件读写 觉得有用的话,欢迎一起讨论相互学习~Follow Me Matlab读取和保存mat文件数据 在matlab命令行中输入save 变量名a,将a变量保存在新生成的a. ...

  2. Python - Django - ORM 聚合查询和分组查询

    models.py: from django.db import models # 出版社 class Publisher(models.Model): id = models.AutoField(p ...

  3. [转]gcc的__builtin_函数介绍

    链接地址:https://blog.csdn.net/jasonchen_gbd/article/details/44948523

  4. react——Table组件

    /* * 构建数据列 * */ tableColumns = (currentData) => { let group = lodashGroupBy(currentData, 'level1' ...

  5. CentOS8安装docker

    参考:https://www.cnblogs.com/ding2016/p/11592999.html 一,安装环境查看 二,下载docker-ce的repo curl https://downloa ...

  6. 海思HI2115芯片-NB-IOT模块向外发短信测试

    1. 说是有短信这个功能,测试下怎么使用?先使能BIP功能 AT+NCONFIG=ENABLE_BIP,TRUE 给SIM卡上电 AT+NUICC= 查询下短信中心服务号码 AT+CSCA? 发送短信 ...

  7. python logging模块日志输出

    import logging logger = logging.getLogger(__name__) logger.setLevel(level = logging.INFO) handler = ...

  8. 解决jQuery版本冲突

    解决jquery版本冲突问题 <!-- 引入1.6.4版的jq --><script src="http://ajax.googleapis.com/ajax/libs/j ...

  9. Image Perimeters

    Description 给出一张由"x"和"."组成的矩阵.每个"x"可以向上下左右及两个斜对角进行连通,请问由某个点开始的"x& ...

  10. KEGG富集分析散点图.md

    输入数据格式 pathway = read.table("kegg.result",header=T,sep="\t") pp = ggplot(pathway ...