MVC3换肤

来源:互联网 发布:打开照片的软件 编辑:程序博客网 时间:2024/05/20 23:05

     换肤方法网上有各种方法,从没弄过,下面介绍看到过的一个方法

在项目App_code文件加下创建cshtml文件,如theme.cshtml,内容如下:

@helperCSS(string cssFile)
    {
    <link rel="stylesheet" href="/Content/@cssFile" type="text/css"  />
}
@helper Theme(string themeName)
    {
    <link rel="stylesheet" href="/Content/themes/@themeName/style.css" type="text/css"  />
}
@helper Script(string scriptFile)
    {
    <script type="text/javascript" src="/Scripts/@scriptFile"></script>
}
@helper jQuery()
    {
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js" type="text/javascript"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.js"
        type="text/javascript"></script>
}

在share的_layout.cshtml中的head部分

<head>
    <title>ASP.Net MVC App Theme</title>
    @theme.CSS("base.css")
    @theme.Theme(”red“)  //red是theme文件名,这个地方可以改为@theme.Theme(ViewBag.theme),通过controller控制Viewbag.theme的值,可以动态改变样式
    @theme.jQuery()
    @theme.Script("imgcrop.js")
</head>

在项目文件夹content/themes/下定义不同的theme

原创粉丝点击