简体   繁体   English

可以在 ASP.NET MVC5 应用程序中覆盖引导程序 CSS 吗?

[英]Can Override Bootstrap CSS in ASP.NET MVC5 Application?

I am relatively new to using ASP.NET MVC5.我对使用 ASP.NET MVC5 比较陌生。 It is nice that Bootstrap is built in but it seems to be very abrasive to altering the CSS based on the Site.css file.内置 Bootstrap 很好,但基于 Site.css 文件更改 CSS 似乎非常粗糙。

My style sheet (Site.css) is in the Content folder in my solution.我的样式表 (Site.css) 在我的解决方案的Content文件夹中。

Similar questions are out there but those are the answers I have tries (show below and obviously having the file AFTER the bootstrap...)那里有类似的问题,但这些是我尝试过的答案(如下所示,显然在引导程序之后有文件......)

I have had some success using just the inline CSS and jQuery but I want to be able to use my style sheet.我仅使用内联 CSS 和 jQuery 取得了一些成功,但我希望能够使用我的样式表。

It seemed that switching from relying on the bundle to this line...似乎从依赖捆绑包切换到这条线......

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

for the reference was better fit... but it STILL doesn't consistently update to my styling.因为参考更合适......但它仍然没有持续更新我的样式。

so then after some digging through old questions I found this...所以在挖掘了一些旧问题之后,我发现了这个......

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

and it seems like one in a while it works but NOT consistently...它似乎偶尔会起作用,但并非始终如一......

I thought at first it had to do with the strongly types bootstrap file with the elements getting more specific styling points, but even when experimenting with the strongest id/elementname/nesting combinations I could it STILL wouldn't work.起初我认为它与强类型引导文件有关,元素获得更具体的样式点,但即使在尝试最强的 id/elementname/nesting 组合时,它仍然无法工作。 (i want to do hovers and such, but even easy things like changing the text color don't work) (我想做悬停之类的,但即使是像改变文本颜色这样简单的事情也不起作用)

Is there some giant flaw in this strategy?这个策略有什么巨大的缺陷吗?

Is there something I'm missing?有什么我想念的吗?

To the best of my knowledge this shouldn't be a huge problem, I would appreciate it greatly for someone to help me out or at least tell me why I'm sooooo wrong.据我所知,这应该不是一个大问题,如果有人能帮助我或至少告诉我为什么我错了,我将不胜感激。

Thanks.谢谢。

You could update the style bundle, even remove all the bootstrap css and add yours.您可以更新样式包,甚至删除所有引导程序 css 并添加您的。 Same for the Javascript bundles. Javascript 捆绑包相同。

https://www.tutorialsteacher.com/mvc/stylebundle-mvc https://www.tutorialsteacher.com/mvc/stylebundle-mvc

The include section listed in the bundle is the list of files that will be used for the specific bundle which you reference by name in your razor pages as you did in your example.捆绑包中列出的包含部分是将用于特定捆绑包的文件列表,您在 razor 页面中按名称引用,就像您在示例中所做的那样。

Just to make sure: there's nothing about ASP.Net/MVC etc that imposes anything about styles.只是为了确保:没有任何关于 ASP.Net/MVC 等强加关于 styles 的任何内容。

  • You can use Bootstrap or not, or any other styling framework您可以使用或不使用 Bootstrap,或任何其他样式框架
  • Bootstrap isn't "built in", it's just the default when an MVC application is scaffolded for you Bootstrap 不是“内置”的,它只是为您搭建MVC应用程序时的默认设置

That out of the way, you don't have to use Bootstrap if you don't want to.顺便说一句,如果您不想使用,则不必使用 Bootstrap。

  • /App_Start/BundleConfig.cs - this is where you define what CSS and javascript frameworks, files you want to be bundled with your application. /App_Start/BundleConfig.cs - 这是您定义 CSS 和 javascript 框架的地方,您希望与应用程序捆绑的文件。 Modify (add/remove/etc) it to your needs.根据您的需要修改(添加/删除/等)它。

Other than that, overriding CSS classes is pretty much the same as it is anywhere (nothing is imposed by the ASP.net framework).除此之外,覆盖 CSS 类与在任何地方几乎相同(ASP.net 框架没有强加任何东西)。

Hth Hth

Although the solution provided by EdSF is good it is still not very understandable for someone who has just started with mvc5.虽然 EdSF 提供的解决方案很好,但对于刚开始使用 mvc5 的人来说仍然不是很理解。 So I will try to explain it a little bit more.所以我会试着多解释一点。

Go to Go 至

/App_Start/BundleConfig.cs

this is the file where all your stylesheets are bundled.这是捆绑所有样式表的文件。

bundles.Add(new StyleBundle("~/bundles/css").Include(
                  //"~/Content/css/bootstrap.min.css",
                     "~/Content/css/Site.css",
                  "~/Content/css/jquery-ui-1.10.4.min.css"));

comment out the line of bootstrap if you don't want to use it and add you css just like the above.如果您不想使用它,请注释掉引导程序行并像上面一样添加 css。 But remember It will remove bootstrap completely and unless you have written you own bootstrap-ish stylesheet for responsive webpage design, you should keep it.但请记住,它将完全删除引导程序,除非您为响应式网页设计编写了自己的引导程序样式表,否则您应该保留它。

When your view page is rendered in the browser inspect the element that's design is not according to your stylesheet and check the class name on that element and override that class with your own stylesheet class and add !important tag on the style attribute so that it will not be overidden by anything else.当您的视图页面在浏览器中呈现时,检查设计不符合您的样式表的元素并检查该元素上的 class 名称并用您自己的样式表!important覆盖该 class不会被其他任何东西覆盖。

Try these steps.试试这些步骤。 Good luck.祝你好运。

RESOLVED: what seemed to be the main issue was not clearing my cache between tests.已解决:似乎主要问题是没有在测试之间清除我的缓存。 Although this seems a little tedious, I was finally able to get some Styling in.虽然这看起来有点乏味,但我终于能够获得一些样式。

but there were 2 more things I tried (from other question on Stack-Overflow) which seemed to all combine to getting it working better.但是我尝试了另外 2 件事(来自 Stack-Overflow 上的其他问题),它们似乎都结合起来让它更好地工作。 (though it would still be nice if changes consistently reloaded on refresh without restarting the app! (I run parallels so it takes quite a long time!) (尽管如果在刷新时始终重新加载更改而不重新启动应用程序,那仍然会很好!(我运行并行,所以需要相当长的时间!)

ANYWAYS!无论如何! The (3) changes I made that were most effective我做出的最有效的 (3) 改变

  1. Clearing the cache (so the browser is forced to re-render the script清除缓存(因此浏览器被迫重新渲染脚本
  2. Changing the name in the "Bundle_Config" from "site/css" to match "Site/css" (not positive why it is like this in the scaffold.. or if it matters...but it seemed to make communication with the file more consistent.将“Bundle_Config”中的名称“site/css”更改为匹配“Site/css” (不肯定为什么它在脚手架中是这样的......或者它是否重要......但它似乎与文件进行通信更一致。
  3. Adding another reference ( THIS HELPED THE MOST ) adding an additional stylesheet reference to the relative path aside from the built in "renderStyles" from the budleconfig that takes place in the _Layout.cshtml.添加另一个引用这最有帮助)除了发生在 _Layout.cshtml 中的 budleconfig 中的内置“renderStyles”之外,还添加了一个对相对路径的附加样式表引用。

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

granted I still need to clear the cache once in a while and restart the app to see changes, I am at least able to change the styling...授予我仍然需要偶尔清除缓存并重新启动应用程序以查看更改,我至少能够更改样式...

thanks for the help everyone!感谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM