Javascript 未定义$reference
我知道这意味着没有加载jquery,但是我的代码似乎是正确的 My_layouts.cshtmlJavascript 未定义$reference,javascript,jquery,asp.net,asp.net-mvc,Javascript,Jquery,Asp.net,Asp.net Mvc,我知道这意味着没有加载jquery,但是我的代码似乎是正确的 My_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
<!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>
@ViewBag.Title-我的ASP.NET应用程序
@style.Render(“~/Content/css”)
@Scripts.Render(“~/bundles/modernizer”)
@ActionLink(“应用程序名”、“索引”、“主页”、null、新{@class=“navbar品牌”})
- @ActionLink(“主页”、“索引”、“主页”)
- @ActionLink(“属性”、“属性”、“主页”)
- @ActionLink(“应用程序属性”、“GetAzureAadApp”、“ActiveDirectory”)
- @ActionLink(“创建应用程序属性”、“CreateProperty”、“ActiveDirectory”)
- @ActionLink(“获取扩展属性”、“获取属性”、“活动目录”)
- @ActionLink(“CreateUser”、“CreateUser”、“ActiveDirectory”)
- @ActionLink(“TestRestCall”、“TestRestCall”、“ActiveDirectory”)
@Html.Partial(“_LoginPartial”)
@RenderBody()
&抄袭@DateTime.Now.Year-我的ASP.NET应用程序
@Scripts.Render(“~/bundles/jquery”)
@Scripts.Render(“~/bundles/bootstrap”)
@RenderSection(“脚本”,必需:false)
我的看法
@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>
@model PruebasAD.Models.SuccessViewModel
@{
ViewBag.Title=“TestRestCall”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
TestRestCall
@模型消息
$(文档).ready(函数(){
var str=JSON.stringify(@(新的MvcHtmlString(Model.JSON)),未定义,2);//缩进级别=2
$('#json result').html(str);
console.log(str);
});
还有我的控制器动作
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();
}
}
public异步任务TestRestCall()
{
Uri serviceRoot=新Uri(AzureAdGraphHapiendPoint);
var token=await GetAppTokenAsync();
字符串requestUrl=”https://graph.windows.net/mysaasapp.onmicrosoft.com/users?api-版本=2013-04-05”;
HttpClient hc=新的HttpClient();
hc.DefaultRequestHeaders.Authorization=新系统.Net.Http.Headers.AuthenticationHeaderValue(“承载者”,令牌);
HttpResponseMessage hrm=await hc.GetAsync(新Uri(requestUrl));
if(人力资源管理ISSUCCESS状态代码)
{
字符串jsonresult=await hrm.Content.ReadAsStringAsync();
返回视图(“TestRestCall”,新的SuccessViewModel
{
Name=“标题”,
Message=“消息”,
JSON=jsonresult.ToJson()
});
}
其他的
{
返回视图();
}
}
尝试移动您的
@Scripts.Render("~/bundles/jquery")
进入
部分,以便在尝试访问它之前加载它。将允许页面继续加载,而无需等待jQuery完成加载()
@ViewBag.Title-我的ASP.NET应用程序
@style.Render(“~/Content/css”)
@Scripts.Render(“~/bundles/modernizer”)
@Scripts.RenderFormat(“,“~/bundles/jquery”)
尝试移动您的
@Scripts.Render("~/bundles/jquery")
进入
部分,以便在尝试访问它之前加载它。将允许页面继续加载,而无需等待jQuery完成加载()
@ViewBag.Title-我的ASP.NET应用程序
@style.Render(“~/Content/css”)
@Scripts.Render(“~/bundles/modernizer”)
@Scripts.RenderFormat(“,“~/bundles/jquery”)
您的脚本正在JQuery上运行。在下面添加一个脚本部分,将JQuery加载到模板文件中。然后在视图中,用页面特定的脚本填充该部分。这将把脚本放在JQuery下面,并将脚本放在底部,以帮助处理页面加载时间您的脚本在JQuery上面运行。在下面添加一个脚本部分,将JQuery加载到模板文件中。然后在视图中,用页面特定的脚本填充该部分。这将把脚本放在JQuery下面,并将脚本放在底部,以帮助页面加载时间您不必按照@CupawnTae的建议将@scripts
声明移动到
中
相反,您可以将视图中的脚本移动到脚本部分(在布局中配置)
@model PruebasAD.Models.SuccessViewModel
@{
ViewBag.Title=“TestRestCall”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
TestRestCall
它讨论了关于放置标记的更好的实践。您不必按照@CupawnTae的建议将@脚本
声明移动到
中
相反,您可以将视图中的脚本移动到脚本部分(在布局中配置)
@model PruebasAD.Models.SuccessViewModel
@{
ViewBag.Title=“TestRestCall”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
TestRestCall
它讨论了关于放置标签的更好实践。@mason感谢您的建议。不幸的是,我对剃须刀的了解还不够,无法做到这一点。谷歌找到了这个答案——这仍然是最好的方式吗?是的,这对我来说是个好办法。哦,另外,您可能想使用(或提及)一个部分来完成这项工作,因为标题是在版面中定义的,将jQuery放入版面将意味着每个页面现在都将引用jQuery,这可能是OP想要的,也可能不是OP想要的。@mason再次感谢,添加了延迟,但我需要多读一点书,然后才能轻松地谈论部分
s@mason谢谢你的建议。不幸的是,我对剃须刀的了解还不够,无法做到这一点。谷歌找到了这个答案——这仍然是最好的方式吗?是的,这对我来说是个好办法。哦
@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>
}