C# IIS 部署后样式捆绑不起作用 (MVC 4)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18946685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:39:41  来源:igfitidea点击:

Style bundling not working after IIS deployment (MVC 4)

c#cssasp.net-mvc-4iis

提问by Omri

I'm having troubles with my style sheets bundling after deployment to IIS. I've created a simple solution to demonstrate my problem.

部署到 IIS 后,我在捆绑样式表时遇到了麻烦。我创建了一个简单的解决方案来演示我的问题。

I've created a simple test project (VS 2012, MVC 4) with a single controller and a view containing an "Hello World" string.

我创建了一个简单的测试项目(VS 2012,MVC 4),其中包含一个控制器和一个包含“Hello World”字符串的视图。

I've created a (test) CSS under the content folder with simple simple color changing

我在内容文件夹下创建了一个(测试)CSS,并使用简单的颜色更改

Content\helloWorldCss\helloWorldStyle.css

Then, I've edited my BundleConfig.csclass and added the path to my CSS as a new bundle:

然后,我编辑了我的BundleConfig.cs类并将我的 CSS 路径添加为一个新包:

            bundles.Add(new StyleBundle("~/Content/helloWorldCss").Include("~/Content/helloWorldCss/helloWorldStyle.css"));

Then, I've added the new bundle to my the _Layout.cshtml:

然后,我已将新包添加到我的_Layout.cshtml 中

@Styles.Render("~/Content/helloWorldCss")

When I run my application via VS (or Page inspector) my CSS is being applied successfully and everything seems to be OK. However, when I publish/deploy my project to IIS (through VS), I can view my HTML but my CSS is not being applied. The following file exists after deployment:

当我通过 VS(或页面检查器)运行我的应用程序时,我的 CSS 被成功应用,一切似乎都正常。但是,当我将我的项目发布/部署到 IIS(通过 VS)时,我可以查看我的 HTML 但我的 CSS 没有被应用。部署后存在以下文件:

Content\helloWorldCss\helloWorldStyle.css

What really puzzles me is that when I alter my _Layout.cshtml and add a "regular" ref to the same CSS instead of using the bundle ref, the CSS is applied after publishing without any issues.

真正让我感到困惑的是,当我更改 _Layout.cshtml 并将“常规”引用添加到相同的 CSS 而不是使用捆绑引用时,CSS 在发布后应用,没有任何问题。

<link href="@Url.Content("~/Content/helloWorldCss/helloWorldStyle.css")" rel="stylesheet" type="text/css" />*

I will appreciate any help and advice on this.

我将不胜感激对此的任何帮助和建议。

采纳答案by Ufuk Hac?o?ullar?

I think you've got a name collision here. ASP.NET MVC will create a file on http://example.org/Content/helloWorldCssafter minification and you already have a folder with the same path. Can you try it again after renaming your bundle?

我认为您在这里遇到了名称冲突。缩小后,ASP.NET MVC 将在http://example.org/Content/helloWorldCss上创建一个文件,并且您已经有一个具有相同路径的文件夹。重命名捆绑包后您可以再试一次吗?

BundleConfig.cs:

捆绑配置.cs:

bundles.Add(new StyleBundle("~/Content/helloWorld").Include("~/Content/helloWorldCss/helloWorldStyle.css"));

_Layout.cshtml:

_Layout.cshtml:

@Styles.Render("~/Content/helloWorld")

回答by shiroxx

This is what i do. IIS Config>Authentication>RightClickOn Anonymous Auth>Click Edit> Check Application pool identity

这就是我所做的。IIS 配置>身份验证>右键单击匿名身份验证>单击编辑>检查应用程序池标识

IIS Config>Authentication>RightClickOn Anonymous Auth>Click Edit>Check Application pool identity

IIS 配置>身份验证>右键单击匿名身份验证>单击编辑>检查应用程序池标识

回答by Yini

When you use VS publish to a test server, it uses defaultAppPool. For the styling and SimpleMembership to work you need:

当您使用 VS 发布到测试服务器时,它使用 defaultAppPool。要使样式和 SimpleMembership 起作用,您需要:

  1. Install ASP.NET 4.0 on your server. cmd -- cd C:\Windows\Microsoft.NET\Framework\v4.0.30319\ Type aspnet_regiis.exe -ir

  2. Add an ASP.NET 4.0 app pool in IIS.

  3. Set your site to use ASP.NET 4.0 as app pool.

  4. Add an ASP.NET 4.0 security login in SQL Server and give it dbcreate role.

  1. 在您的服务器上安装 ASP.NET 4.0。cmd -- cd C:\Windows\Microsoft.NET\Framework\v4.0.30319\ 输入aspnet_regiis.exe -ir

  2. 在 IIS 中添加 ASP.NET 4.0 应用程序池。

  3. 将您的站点设置为使用 ASP.NET 4.0 作为应用程序池。

  4. 在 SQL Server 中添加一个 ASP.NET 4.0 安全登录并为其赋予 dbcreate 角色。