我知道这意味着jquery没有加载,但是我的代码似乎是正确的。
我的_layouts.cshtml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("Application name", "Index", "Home", null, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Property", "Property", "Home")</li>
<li>@Html.ActionLink("App Properties", "GetAzureAadApp", "ActiveDirectory")</li>
<li>@Html.ActionLink("Create App Properties", "CreateProperty", "ActiveDirectory")</li>
<li>@Html.ActionLink("Get Extended Properties", "GetProperties", "ActiveDirectory")</li>
<li>@Html.ActionLink("CreateUser", "CreateUser", "ActiveDirectory")</li>
<li>@Html.ActionLink("TestRestCall", "TestRestCall", "ActiveDirectory")</li>
</ul>
@Html.Partial("_LoginPartial")
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>我的观点
@model PruebasAD.Models.SuccessViewModel
@{
ViewBag.Title = "TestRestCall";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>TestRestCall</h2>
<article>
<aside class="green">
@Model.Message
</aside>
<aside>
<pre id="json-result">
</pre>
</aside>
</article>
<script type="text/javascript">
$(document).ready(function(){
var str = JSON.stringify(@(new MvcHtmlString(Model.JSON)), undefined, 2); // indentation level = 2
$('#json-result').html(str);
console.log(str);
});
</script>和我的控制员的行动
public async Task<ActionResult> TestRestCall()
{
Uri serviceRoot = new Uri(azureAdGraphApiEndPoint);
var token = await GetAppTokenAsync();
string requestUrl = "https://graph.windows.net/mysaasapp.onmicrosoft.com/users?api-version=2013-04-05";
HttpClient hc = new HttpClient();
hc.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage hrm = await hc.GetAsync(new Uri(requestUrl));
if (hrm.IsSuccessStatusCode)
{
string jsonresult = await hrm.Content.ReadAsStringAsync();
return View("TestRestCall", new SuccessViewModel
{
Name = "The Title",
Message = "The message",
JSON = jsonresult.ToJson()
});
}
else
{
return View();
}
}发布于 2015-05-23 00:35:11
您不必像@CupawnTae建议的那样将@Scripts声明移动到<head>中。
相反,您可以将视图中的脚本移动到脚本部分(在布局中进行配置)。
@model PruebasAD.Models.SuccessViewModel
@{
ViewBag.Title = "TestRestCall";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>TestRestCall</h2>
<article>
<aside class="green">
@Model.Message
</aside>
<aside>
<pre id="json-result">
</pre>
</aside>
</article>
@section scripts{
<script type="text/javascript">
$(document).ready(function(){
var str = JSON.stringify(@(new MvcHtmlString(Model.JSON)), undefined, 2); // indentation level = 2
$('#json-result').html(str);
console.log(str);
});
</script>
}在您的布局中,由于您正在将脚本部分呈现在jQuery引用下面,所以它们将按正确的顺序加载。
之所以如此重要,是因为它将在页面的主HTML之后加载脚本。页面的加载速度似乎更快,因为用户会更快地看到站点。这在具有较小HTML文档和CSS引用的站点中工作得很好。
但是,您也可能对在HTML标记中放置标记的最佳位置是哪里?感兴趣,因为它讨论了有关放置标记的更好实践。
发布于 2015-05-23 00:27:36
试着移动你的
@Scripts.Render("~/bundles/jquery")进入<head>部分,以便在尝试访问它之前加载它。属性将允许页面继续加载,而无需等待jQuery完成加载(在支持的浏览器上)。
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.RenderFormat("<script src='{0}' defer='defer'></script>","~/bundles/jquery")
</head>发布于 2015-05-23 00:30:50
您的脚本运行在JQuery之上。在模板化文件中加载JQuery的下面添加一个脚本部分。然后,在视图中,用特定于页面的脚本填充该部分。这将将脚本置于JQuery下面,并将脚本保持在底部,以帮助处理页面加载时间。
https://stackoverflow.com/questions/30407618
复制相似问题