手记

ASP.NET MVC中切换模板页(不同目录的cshtml文件)

看来以后建立一个父类控制器还是有必要的。。。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NETMVC切换模板.Controllers
{
public class BaseController : Controller
{
protected override void OnResultExecuting(ResultExecutingContext filterContext)
{
ViewResult viewResult = filterContext.Result as ViewResult;
if (viewResult != null)
{
string x = Request.QueryString["x"];
string text = string.IsNullOrEmpty(x) ? "" : "/Templete/Blue";
string arg = string.IsNullOrEmpty(text) ? "" : text;
string arg2 = filterContext.RequestContext.RouteData.Values["Controller"].ToString();
string arg3 = filterContext.RequestContext.RouteData.Values["Action"].ToString();
if (string.IsNullOrWhiteSpace(viewResult.ViewName))
{
viewResult.ViewName = string.Format("{0}/Views/{1}/{2}.cshtml", arg, arg2, arg3);
return;
}
}
base.OnResultExecuting(filterContext);
}
}
}

源码下载:
http://ohpxbzczu.bkt.clouddn.com/ChangeMuBan.zip

0人推荐
随时随地看视频
慕课网APP