【问题标题】:Why is the Bootstrap class not being applied?为什么没有应用 Bootstrap 类?
【发布时间】:2016-04-04 17:45:06
【问题描述】:

在 ASP.NET 应用程序中提供的默认/通用 html 中,主页使用 Index.cshtml 中的 html 呈现,开始:

<div class="jumbotron">
    <h1>ASP.NET</h1>
    <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS, and JavaScript.</p>
    <p><a href="http://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
</div>

这会在页面顶部创建一个“jumbotronified”部分,其中包含一个大的“ASP.NET”。

但是,当我尝试在另一个地方使用该引导 css 类(“jumbotron”)时:

StringBuilder builder = new StringBuilder();
builder.Append("<html>");
builder.Append("<head>");
builder.Append("<title>");
builder.Append(string.Format("Available Delivery Performance Reports For 
{0}", unit));
builder.Append("</title>");
builder.Append("</head>");
builder.Append("<body>");

builder.Append("<div class=\"jumbotron\">");
builder.Append(String.Format("<h1>{0}</h1>", unit.ToUpper()));
builder.Append("</div>");
. . .
builder.Append("</table>");
builder.Append("</body>");
builder.Append("</html>");

return builder.ToString();

我没有什么特别的 - 渲染了 jumbotronned h1,但没有 jumbotron“效果”,我认为如果引导类可以从项目中的一个地方获得,那么它们将在项目中的任何地方都可用。我需要做什么才能将这个动态 html 引入引导子系统?

【问题讨论】:

  • 我在 StringBuilder 中看不到 bootstrap.min.cssbootstrap.min.js
  • 你是如何包含引导库的?
  • @Aziz:我以为它已经包含在内了,因为它正在主页上使用。
  • 如何在现有的 html 标签中渲染动态 html 标签?你在使用 iframe 吗?

标签: css asp.net twitter-bootstrap asp.net-web-api


【解决方案1】:

看起来您正在为整个新页面生成 CSS(即不从布局继承且不是局部视图的页面)。

如果是这种情况,那么您需要在这个新生成的页面中明确引用您的引导文件,以便正确应用它们:

你可能会使用类似的东西:

// Example of adding in Bootstrap CSS
builder.AppendFormat("<link href='{0}' rel='stylesheet' />", Url.Content("~/Style/bootstrap.min.css"));
// Example of appending jQuery JS
builder.AppendFormat("<script src='{0}'></script>",Url.Content("~/Scripts/jquery.min.js"));
// Example of appending Bootstrap JS
builder.AppendFormat("<script src='{0}'></script>",Url.Content("~/Scripts/bootstrap.min.js"));
builder.Append("</head>");

传统上,这些文件将在 Layout 或普通视图的 &lt;head&gt; 部分中引用,但是由于您在 StringBuilder 中手动构建整个 HTML 内容,因此您需要在其中包含任何引用作为嗯。

【讨论】:

  • 这是一个错字,对不起。它应该是Url.Content
  • 与 Url.Content 相同。 “网址。”只给我“Equals”和“ReferenceEquals”。识别/自动使用的“Url”是“System.Security.Policy.Url”我应该使用不同的吗?
  • 你在使用 MVC 吗?如果是这样,则 Url.Content() 方法应该在 System.Web.Mvc 命名空间中可用。您可以尝试添加 using 语句来包含它。如果您使用的是 Web 表单,则可以使用替代选项,即 ResolveUrl()
  • 我注释掉了“使用 System.Security.Policy.Url”(我没有明确添加),并添加了“使用 System.Web.MVC”,但它是灰色的。当我添加“System.Web.Mvc”时。我看到“UrlHelper”和“UrlParameter”,但被提示导入 System.Security.Policy.Url
【解决方案2】:

这(引用引导程序和 jQuery CDN)有效:

. . .
builder.Append("</title>");
builder.Append("<link href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css' rel='stylesheet' />");
builder.Append("<script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js'></script>");
builder.AppendFormat("<script src=\"https://code.jquery.com/jquery-1.12.2.min.js\" integrity=\"sha256-lZFHibXzMHo3GGeehn1hudTAP3Sc0uKXBXAzHX1sjtk=\" crossorigin=\"anonymous\"></script>");
builder.Append("</head>");
. . .

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 2012-04-26
    相关资源
    最近更新 更多