我刚刚创建了一个ASP.NET MVC 4项目,并使用Visual Studio 2012 RC创建了一个Controller和Razor Views for Index和CreateActions.
当我来运行应用程序并浏览到Create视图时,显示以下错误:
编译器错误消息:CS0103:当前上下文中不存在名称"脚本"
问题是以下代码自动添加到视图的底部:
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Run Code Online (Sandbox Code Playgroud)
为什么Scripts不存在?
我查看了Assembly System.Web.Mvc.dll,v4.0.0.0中的基本Web页面类
我可以看到以下帮助器属性:
但没有任何一个名为Scripts.
有任何想法吗?
编辑:
我的Web.config文件看起来像这样(不受Visual Studio创建的那个):
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
Run Code Online (Sandbox Code Playgroud)
编辑#2:
人们正在写关于使用@Scripts帮助者的博客:
然而,刚刚将Visual Studio 2012 RC安装到新的Windows 8安装上,@Scripts即使Visual Studio将其添加到生成的View中,我仍然无法使用!
解决方案如下.
我不知道如何关闭它,因为最终更新似乎解决了这个问题.我仔细检查了我使用新项目执行了干净安装.但是,我所做的同样失败的项目现在可以在各种更新后进行,并且没有人工明显的干预.感谢所有的想法,但当时肯定存在问题;)
我有一个ASP.Net MVC项目,我想实现捆绑,但一切我可以在互联网上找到指引我开BundleConfig.cs的App_Start-但是这个文件不在我的项目存在.我只有三个文件夹中的文件:FilterConfig,RouteConfig和WebApiConfig.
我创建解决方案时没有生成Bundle配置(IIRC在开始时它是一个空白的ASP.NET MVC项目).
看起来这应该很容易做到,但我只是简单无法弄明白.
PS只是为那些不仔细阅读的人澄清,这是针对从头开始创建的MVC4/.Net 4.5应用程序.解决方案标记如下.
在我的mvc应用程序中,在_Layout.cshtml中有以下代码...
<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>
Run Code Online (Sandbox Code Playgroud)
但问题是它说The name 'Scripts' does not exists in the current context.
我已经将程序集添加到引用,并将Bundle配置添加为 using System.Web.Optimization;
@styles也会发生这种情况..我该怎么办?
我正在开发一个在Visual Studio 2010中启动的MVC 4项目.现在我正在使用Visual Studio 2012开发一台机器,因为我无法访问我最初使用的机器.我整个上午都试着找到答案,但它们似乎对我的情况没有帮助.
我按照如何为MVC-3-converted-to-4 app添加对System.Web.Optimization的引用,并从nuget安装到我的解决方案中.即使我已经在我的机器上下载并安装了所有必需的参考包,System.Web.Optimization仍然会一直缺失.还有什么我可以做的吗?
编辑:当我尝试构建项目时,我收到以下错误
我的BundleConfig.cs包含
using System.Web;
using System.Web.Optimization;
namespace BCCDC_AdminTool
{
public class BundleConfig
{
// For more information on Bundling, visit http://go.microsoft.com/fwlink/? LinkId=254725
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
"~/Scripts/jquery-ui-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
// Use the development version of Modernizr to develop with and learn from. Then, when you're
// ready for production, use …Run Code Online (Sandbox Code Playgroud) 我正在部署一个新网站作为我的主要网站,它的工作非常精彩.
根网址下的所有应用程序都可以正常工作,除了一个.它是一个遗留系统(c#.net),使用频繁,遗憾的是,我无法访问源代码.
当我运行遗留应用程序www.mysite.com/crm时,出现以下错误:
Server Error in '/crm' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to
service this request. Please review the following specific error details and modify
your source code appropriately.
Compiler Error Message: CS0234: The type or namespace name 'Optimization' does not
exist in the namespace 'System.Web' (are you missing an assembly reference?)
Source Error:
Line 17: <pages>
Line 18: <namespaces>
Line 19: <add namespace="System.Web.Optimization" />
Line 20: </namespaces>
Line 21: <controls> …Run Code Online (Sandbox Code Playgroud) 我Microsoft.Web.Optimization通过Visual Studio 2012 试用版已经使用了一段时间了.我认为优化命名空间将是.NET 4.5的一部分,如此处所述.
但是,我安装了Visual Studio 2012和.NET Framework 4.5的新版本,现在System.Web.Optimization默认没有命名空间!我不得不通过NuGet安装软件包
VS 2012 Ultimate版本尚未完成吗?或者,Microsoft.Web.Optimization命名空间不在.NET 4.5中?
我有用VS2010创建的MVC4 RC项目.我不确定发生了什么,突然间我开始收到以下错误:
错误1"System.Web.Optimization.BundleCollection"不包含关于"RegisterTemplateBundles"的定义和没有扩展方法"RegisterTemplateBundles"接受类型"System.Web.Optimization.BundleCollection"的第一个参数可以找到(是否缺少使用指令或汇编引用?)C:\ xxxx\xxxx\Global.asax.cs 40 33 xxxx
错误来自Application_Start():
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
BundleTable.Bundles.RegisterTemplateBundles();
}
Run Code Online (Sandbox Code Playgroud)
我在Global.asax.cs文件中使用以下语句:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
Run Code Online (Sandbox Code Playgroud)
有任何想法吗????
我有一个MVC4项目,它具有以下结构:
/Content/css/sprites/topbar.css
/Content/images/topbar.png
在css文件中我试图通过使用以下方法引用图像:
background: url('../../images/topbar.png')
Run Code Online (Sandbox Code Playgroud)
但图像不显示.如果我更改它,以便图像位于:
/Content/css/sprites/topbar.png
并将css更改为:
background: url('content/css/sprites/topbar.png')
Run Code Online (Sandbox Code Playgroud)
它有效,但这打破了我的项目结构.
有任何想法吗?
我没有提到别的东西,因为我认为它不相关,但它似乎影响了这一点!
我@System.Web.Optimization.Styles.Render("~/MainStyles")用来捆绑和缩小CSS,但如果我采取了这一步,那么它就像我期望的那样工作.如何使用我的项目结构和使用捆绑来完成所有工作?
using System.Web;
using System.Web.Optimization;
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
"~/Scripts/jquery-ui-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
Run Code Online (Sandbox Code Playgroud)
我收到错误:
Error 16 The type or namespace name 'ScriptBundle' could not be found (are you missing a using directive or an assembly reference?)
Run Code Online (Sandbox Code Playgroud)
看起来像一些参考问题,但我无法弄明白.请帮忙.可能导致此错误的原因是什么?
作为学习练习,我创建了一个空白的MVC4应用程序,并使用它来构建博客类型的网站。
我了解到这可能不是最好的主意...我已将Content和Views\Shared文件夹从我使用默认内容创建的另一个项目复制到我的博客项目中,但是以下几行给我错误:
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
Run Code Online (Sandbox Code Playgroud)
在我的_Layout.cshtml文件中都在红色下划线,错误是:
该名称在当前上下文中不存在
因此,我回到我的“默认”项目中,将鼠标悬停在这些项目上来检查它们的来源。工具提示说
class System.Web.Optimization.Styles
class System.Web.Optimization.Scripts
Run Code Online (Sandbox Code Playgroud)
因此,我按照此问题仔细研究并安装了Web优化框架,但该问题尚未解决。
该项目构建了全部,但是没有应用任何样式,我100%确信脚本控件中的任何内容都不起作用...
谁能帮我解决这个问题?