ASP.NET MVC - 样式表必须放在 Site.Master 文件中吗?
我注意到我无法在任何页面上添加样式表。必须将它们添加到母版页中。
我在主视图中已经有大约 15 个样式表,这似乎有点矫枉过正,因为只有某些页面使用特定的样式表。
我想我可以通过 javascript 引用该文件(尽管我想不出如何实现),但如果不必使用任何文件,那就太好了。
I noticed that I cannot add stylesheets on any page. They must be added to the master page.
I already have about 15 stylesheets in the master view, which seems like overkill, since only some of the pages use a certain stylesheet.
I imagine I could reference the file via javascript (although, I can't think of how off the top of my head), but it would be really nice to not have to use any.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该能够将它们添加到内容页面上的头部占位符...
MasterPage:
内容页面:
You should be able to add them to the head placeholder on the content pages...
MasterPage:
Content Page:
不,它们不必进入母版页。
您可以在母版页中为它们创建内容占位符,然后使用页面上的内容元素添加特定于页面的样式。
例如,在主控中:
然后在页面中:
No, they don't have to go in the master page.
You can create a content placeholder for them in the master page, and then add page-specific styles using a content element on the page.
E.g., in the master:
Then in the page:
你能展示一些代码吗?
实际上,视图中可以有 CSS 样式表。但问题是这是否是一个好的做法。最好的想法是在母版页的 Head 部分中创建一个占位符,并在视图中使用此占位符来使用正确的 CSS 文件。
像这样:
在您的母版页中:
在您的视图中:
Can you show some code?
It's actually possible to have a CSS stylesheet in the view. But the question is if it's a good practice. The best idea is to create a placeholder in the masterpage in the Head section and use this placeholder in the view to use the correct CSS files.
Like this:
In your masterpage:
Inside your view:
您可以将样式表以及图像和控件外观存储为主题的一部分< /a> 并在每个内容页面上指定应使用哪个主题。 注意:您无法在 MasterPage 本身中指定主题。它仅适用于页面指令。
请注意,这需要 head 元素为
runat="server"
才能让 ASP.NET 自动向页面添加适当的样式表引用。我不确定这是否适用于 ASP.NET MVC,但您正在使用 MasterPage...
You can store stylesheets, along with images and control skins, as part of a Theme and specify on each content page which theme should be used. Note: You can not specify a theme in the MasterPage itself. It only works with Page directives.
Note that this requires the head element to be
runat="server"
to have ASP.NET automagically add the appropriate stylesheet references to the page.I'm not sure if this applies to ASP.NET MVC or not, but you are using a MasterPage...