前言

直接上干货就是,就不废话了。

使用场景:分离模块,多站点等~~

一、分离模块

自定义视图引擎,设置视图路径格式

项目结构图

1.Code: 在Global.asax Application_Start方法中添加自定义的视图引擎

        using System.Collections.Generic;
using System.Web.Mvc;
namespace MvcProjectMain.AreasViewEngine
{ /// <summary>
/// 自定义视图引擎
/// </summary>
/// <remarks>
/// ViewEngines.Engines.Add(new MvcProjectMain.AreasViewEngine.ThemableRazorViewEngine());
/// </remarks>
public class ThemableRazorViewEngine : VirtualPathProviderViewEngine
{
//所有区域分离到Modules文件夹,{2}为区域名
public ThemableRazorViewEngine()
{
ViewEngines.Engines.Clear();
AreaViewLocationFormats = new[]
{
"~/Modules/{2}/Views/{1}/{0}.cshtml",
"~/Modules/{2}/Views/Shared/{0}.cshtml",
}; AreaMasterLocationFormats = new[]
{
"~/Modules/{2}/Views/{1}/{0}.cshtml",
"~/Modules/{2}/Views/Shared/{0}.cshtml",
}; AreaPartialViewLocationFormats = new[]
{
"~/Modules/{2}/Views/{1}/{0}.cshtml",
"~/Modules/{2}/Views/Shared/{0}.cshtml",
}; ViewLocationFormats = new[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml",
}; MasterLocationFormats = new[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml",
};
PartialViewLocationFormats = new[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml",
}; FileExtensions = new[] { "cshtml" };
} protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
string layoutPath = null;
var runViewStartPages = false;
IEnumerable<string> fileExtensions = base.FileExtensions;
return new RazorView(controllerContext, partialPath, layoutPath, runViewStartPages, fileExtensions);
} protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
string layoutPath = masterPath;
var runViewStartPages = true;
IEnumerable<string> fileExtensions = base.FileExtensions;
return new RazorView(controllerContext, viewPath, layoutPath, runViewStartPages, fileExtensions);
}
}
}

2.Code:在Global.asax中添加注册区域-->AreaRegistration.RegisterAllAreas();

        public class ThemesAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "MvcProjectThemes";
}
} public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"MvcProjectThemes",
"MvcProjectThemes/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional, },
namespaces: new string[] { "MvcProjectThemes.Controllers" }
);
}
}

3.Code:注册主项目MvcProjectMain的路由 RouteConfig.RegisterRoutes(RouteTable.Routes);

namespace MvcProjectMain
{
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[] { "MvcProjectMain.Controllers" }
);
}
}
}

最后

主要代码就是步骤1中的ThemableRazorViewEngine.cs类。自定义查找路径,其他的都是MVC的基础知识了,不懂自行查阅资料

asp.net mvc项目自定义区域的更多相关文章

  1. 在 ASP.NET MVC 项目中使用 WebForm、 HTML

    原文地址:http://www.cnblogs.com/snowdream/archive/2009/04/17/winforms-in-mvc.html ASP.NET MVC和WebForm各有各 ...

  2. ASP.NET MVC 项目分离

    ASP.NET MVC 项目分离 说明: ZRT.Web 是前台网站,目录[D:\ZRT.Web\] ZRT.Admin 是后台管理,目录[D:\ZRT.Web\Applications\Admin\ ...

  3. ASP.Net Mvc实现自定义User Identity用户身份识别系统(1)

    目的 当我们新建asp.net mvc 项目时,我们在使用下图所示的代码是否有以下思考: 1,在this.User.Identity.Name,为什么可以使用this便可以选中Name属性: 2,若项 ...

  4. AngularJS2 + ASP.NET MVC项目

    环境:VS2015, NodeJS:v 6.5, npm: v3.10, AngularJs 2 通过将ASP.NET MVC项目与Angualr 2官网上的quick start整合的过程中遇到些问 ...

  5. 远程调试 ASP.NET MVC 项目

    Visual Studio 支持从一台计算机到另一台设备的远程调试.进行远程调试时,主机可以是任何支持 Visual Studio 的平台.远程设备可以是 x86.x64 或 ARM 平台. 本文将指 ...

  6. 习题-任务2初始ASP.NET MVC项目开发

    一.选择题 1.在ASP.NET MVC项目的RouteConfig.cs文件中,(    )方法注册了默认的路由配置. A.RegisterMap    B.RegisterRoutes    C. ...

  7. Asp.net mvc项目架构分享系列之架构概览

    Asp.net mvc项目架构分享系列之架构概览 Contents 系列一[架构概览] 0.项目简介 1.项目解决方案分层方案 2.所用到的技术 3.项目引用关系 系列二[架构搭建初步] 4.项目架构 ...

  8. 1.2 认识ASP.NET MVC项目结构

    1.开发环境 操作系统:xp.vista.windows 7.windows 8.windows server 2003|2008|2008R2|2012: 集成开发环境IDE: Vsiual Stu ...

  9. 2.2 利用项目模板创建ASP.NET MVC项目

    1.启动VS2012,点击“文件|新建|项目”. 2.在新建项目的窗口中,选择ASP.NET MVC 4应用程序. 3.在新ASP.NET MVC 4项目窗口中的“选择模板”列表中选择“基本”模板,在 ...

随机推荐

  1. 如何在没有域的环境中搭建AlwaysOn(二)

    对DBA而言,不需要域就可以搭建SQL Server AlwaysOn是Windows Server 2016中最令人兴奋的功能了,它不仅可以降低搭建的成本,而且还减少了部署和运维的工作量. 上篇博客 ...

  2. lua中实现异步资源读写

    同样还是更新方面的需求,当我们检测到版本是新安装的以后,要进行upd目录清除.如果使用os.execute执行 rm -rf ooxx 是非常快的但由于os.execute一旦报错,那整个lua进程就 ...

  3. 完全移除TFS2013的版本控制

    环境:Vs2103(TFS2013) 目的:去掉别人项目里的TFS控制,因为每次打开时会有提示信息 解决方法: 1.删除隐藏的.$tf文件夹,搜索*.vssscc和*.vspscc这两个后缀的文件,删 ...

  4. MyEclipse打开 HTML 报错Failed to create the part's controls

    拷贝代码时有时会弹出这个错误,页面仍然可以访问,但是无法编辑很郁闷.  MyEclipse默认打开编辑页面是MyEclipse visual html designer 右击html页面选择open  ...

  5. Chrome开发者工具不完全指南(二、进阶篇)

    上篇向大家介绍完了基础功能篇,这次分享的是Chrome开发工具中最有用的面板Sources.  Sources面板几乎是我最常用到的Chrome功能面板,也是在我看来决解一般问题的主要功能面板.通常只 ...

  6. 史上最全的 Redux 源码分析

    前言 用 React + Redux 已经一段时间了,记得刚开始用Redux 的时候感觉非常绕,总搞不起里面的关系,如果大家用一段时间Redux又看了它的源码话,对你的理解会有很大的帮助.看完后,在回 ...

  7. iOS-APP发布应注意

    地区范围文件不用填 电话号码前缀是86

  8. react-native环境搭建

    目标平台 Android 开发平台 windows 开发环境安装建议:由于开发环境存在差异,建议参照react官网 或者react中文网 安装, react-native -- 在Windows下搭建 ...

  9. C#设计模式系列:模板方法模式(Template Method)

    你去银行取款的时候,银行会给你一张取款单,这张取款单就是一个模板,它把公共的内容提取到模板中,只留下部分让用户来填写.在软件系统中,将多个类的共有内容提取到一个模板中的思想便是模板方法模式的思想. 模 ...

  10. 用js实现放大镜的效果

    第一次发博客,还有点小激动,本人现在正在天津上大学,希望以后从事前端这一行业,学习的时间不长,写博客为了记录自己的学习过程和收获,当然也算是巩固.可能写的东西不会像大牛那样高大上,只是一些基本的内容, ...