前言

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

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

一、分离模块

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

项目结构图

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. MySQL 子分区

    200 ? "200px" : this.width)!important;} --> 介绍 子分区其实是对每个分区表的每个分区进行再次分隔,目前只有RANGE和LIST分区 ...

  2. oracle中查询、禁用、启用、删除表外键

    1.查询所有表的外键的: select table_name, constraint_name from user_constraints where constraint_type = 'R';   ...

  3. Objective-C 工厂模式(上) -- 简单工厂模式

    简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,但不属于23种GOF设计模式之一.简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例.简单工厂 ...

  4. Andrew Ng机器学习公开课笔记 -- 学习理论

    网易公开课,第9,10课 notes,http://cs229.stanford.edu/notes/cs229-notes4.pdf 这章要讨论的问题是,如何去评价和选择学习算法   Bias/va ...

  5. easyui相关script的配置

    <!-- 1 jQuery的js包 --> <script type="text/javascript" src="jquery-easyui-1.4. ...

  6. WPF自定义控件与样式(2)-自定义按钮FButton

    一.前言.效果图 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 还是先看看效果 ...

  7. WPF 后台读取样式文件

    ResourceDictionary dic = new ResourceDictionary { Source = new Uri("Styles.xaml",UriKind.R ...

  8. HTTP的RST包与WinHttp延迟关闭TCP连接

    一.RST包也常见于断开TCP连接  几个月前用wireshark抓HTTP包发现有的网络通信在结束的时候没有使用四次握手,而是直接使用RST包.如: 在TCP协议中RST表示复位,用来异常的关闭连接 ...

  9. Hawk 3.1 动态页面,ajax,瀑布流

    不少朋友反映,Hawk的手气不错,好像没法处理动态页面.其实很容易,比其他软件都容易,让我慢慢道来. 1. 什么是动态页面 很多网站,在刷新的时候会返回页面的全部内容,但实际上只需要更新一部分,这样可 ...

  10. Comet ASP.NET AJAX 示例

    最近公司有个项目,里面要求要用到Comet技术,所以就到网上找了一下相关的资料和文章,发现有些人说用Ajax的长轮询比较好,后来就百度了一下,发现comet貌似就是通过ajax演变而来的,也就是com ...