5

阅读this post这里在stackoverflow想编译发布模式时加载不同的CSS。条件编译不起作用

代码:

@{ #if (Debug) 
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> 
#else 
<link href="@Url.Content("~/Content/Site-min.css")" rel="stylesheet" type="text/css" /> 
#endif 
} 

尝试2

@{ #if (Debug) } 
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> 
@{ #else } 
<link href="@Url.Content("~/Content/Site-min.css")" rel="stylesheet" type="text/css" /> 
@{ #endif } 

我试图用大写 来进行调试,但是没有改变编译调试到版本

+2

http://stackoverflow.com/questions/4696175/razor-view-engine-how-to-enter-preprocessorif-debug/4697570#4697570 这个怎么样? – takepara

回答

7

据时没有任何影响this SO post,如果你想要这样的事情在工作中,您可以使用模型中的属性来驱动视图的条件内容,因此C#通过编译时指令来设置模型的布尔值(IsDebug,或其他),而视图依赖于此。

所以,你的模型将最终做这样的事情:

bool IsDebug = true; 

#if (!DEBUG) 
IsDebug = false; 
#endif 

和您查看会做这样的事情:

@if(Model.IsDebug) 
{ 
} 
else 
{ 
} 

你也可以使用ViewBag /的ViewData来保存布尔值太大,我假设。


UPDATE:

基础上您的意见,这里的东西,你可以

创建一个新的BaseController类从Controller继承。

public abstract class BaseController : Controller 
{ 
    ... 
    protected BaseController() 
    { 
     bool indebug = false; 

     #if DEBUG 
     indebug = true; 
     #endif 

     ViewBag.InDebug = indebug; 
    } 
} 

并让您的控制器继承此而不是Controller。

然后在你的_Layout.cshtml你可以做到这一点:

@if (ViewBag.InDebug) 
{ 
} 
else 
{ 
} 

这似乎是工作确定。

+0

问题是,应该为所有页面执行此代码。 什么是所有页面的CSS文件。 一旦控制器只有一个人可能会问,将不得不把所有的控制器,我不想要的。 代码位于_Layout.cshtml(主布局) 为什么我不能使用Direct View? – ridermansb

+0

在我的答案中看到我的其他信息 - 也许它也适用于您。 – itsmatt