Tag: asp.net mvc

有人可以向我解释这个ASP.NET MVC代码块吗?

这是ASP.NET MVC2(RTM) System.Web.Mvc.AuthorizeAttribute类中的当前代码: – public virtual void OnAuthorization(AuthorizationContext filterContext) { if (filterContext == null) { throw new ArgumentNullException(“filterContext”); } if (this.AuthorizeCore(filterContext.HttpContext)) { HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache; cache.SetProxyMaxAge(new TimeSpan(0L)); cache.AddValidationCallback( new HttpCacheValidateHandler(this.CacheValidateHandler), null); } else { filterContext.Result = new HttpUnauthorizedResult(); } } 因此,如果我’授权’然后做一些缓存的东西,否则抛出401 Unauthorized响应。 问题: 这3条缓存线有什么作用? 欢呼:)

C#如何在使用asp.net mvc时设置autopostback属性?

我正在使用asp.net MVC框架。 在我的页面上我有一个dropdwonbox,当点击一个选项时,我想转到另一个页面。 但我找不到如何/在哪里将autopostback属性设置为true。 这是我正在使用的代码: ASPX: 控制器: public ActionResult Index(int id) { Chapter c = new Chapter(); ViewData[“qchap”] = c.GetAllChaptersByManual(id); return View(); } 我需要做什么才能使用自动回复function?

在MVC视图中迭代匿名类型数据

在某些视图数据中我放了一个匿名类型的结果: var projectData = from p in db.Projects orderby p.title select new { Title = p.title, DevURL = p.devURL ?? “N/A”, QAURL = p.qaURL ?? “N/A”, LiveURL = p.liveURL ?? “N/A”, Users = p.GetUsers().MakeUserList() }; ViewData[“ProjectSummary”] = projectData; 如何在前端的MVC视图中迭代这个视图数据来说,制作一个结果表?

MEF2中强类型元数据(System.Composition)

我在新的ASP.NET MVC4项目中使用来自MEF的System.Composition命名空间用于Web和Windowsapp store应用NuGet包 。 我已经读过 ,在MEF2中你不再使用Lazy ,但是现在你必须为元数据视图提供一个具体的类型(并且可能使用ExportFactory 而不是Lazy ?)。 但是,我找不到任何关于如何工作的例子 – 只提到使用具体类型而不是接口。 我已经尝试了一些方法,但不断收到以下错误 – “ 缺少’AccountID’的导出元数据,并且没有提供默认值 ”。 我的代码…… 创建容器(在Global.asax或App_Start文件夹中): // Get assemblies that will be providing imports and exports var assemblies = GetAssemblies(); // Get conventions that will be used to find imports and exports var conventions = GetConventions(); var container = new ContainerConfiguration().WithAssemblies(assemblies, conventions).CreateContainer(); […]

无法加载文件或程序集EntityFramework

我已经部署了一个ASP.NET MVC 4应用程序并且主页加载正常,但是当我尝试访问任何其他页面(都尝试连接到SQL数据库)时,我收到此错误: 无法加载文件或程序集’EntityFramework,Version = 4.1.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089’或其依赖项之一。 定位的程序集的清单定义与程序集引用不匹配。 (来自HRESULT的exception:0x80131040)描述:在执行当前Web请求期间发生了未处理的exception。 请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。 exception详细信息:System.IO.FileLoadException:无法加载文件或程序集’EntityFramework,Version = 4.1.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089’或其依赖项之一。 定位的程序集的清单定义与程序集引用不匹配。 (HRESULTexception:0x80131040) 我检查了Web.config文件,它有以下相关条目: … 我已经阅读了我可以通过Google找到的所有内容,但到目前为止还没有任何帮助。 我知道不知何故,我构建应用程序的EF版本与部署机器上的版本不同,但我可以使用一些方法来纠正这种差异。

如何启用迁移以在MVC4中更新我的数据库?

我正在使用Visual Studio 2012中的MVC4进行项目,并在表中添加了一列。 现在,当我想调试我的项目时,错误说使用迁移来更新我的数据库。 我该怎么办? 我一直在搜索,发现了一些方法,如: protected override void OnModelCreating(DbModelBuilder modelBuilder) { Database.SetInitializer(null); } 但不知道如何以及在何处实现这个…已经尝试过app_start , global.asax等… 我发现的是,从nuget直接在控制台中启用迁移。 但我无法做到这一点。 我使用的命令: Enable-Migrations -EnableAutomaticMigrations ==>控制台说找到了多个上下文。 要启用use,请Enable-Migrations -ContextTypeName NameOfTheNamespace.Models.DefaultConnection 但我不知道什么是-ContextTypeName ,已经尝试了很多但无法理解。 My Model Code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; using System.Data.Entity.ModelConfiguration.Conventions; using System.Data.Entity.Migrations; using System.ComponentModel.DataAnnotations; using System.Data.Entity.Infrastructure; namespace Vista.Models { public class TabelaIndex […]

如何关闭MVC请求的缓存,而不是IIS7中的静态文件?

我正在开发一个ASP.NET MVC应用程序。 大多数控制器操作都不应该被缓存。 因此我在Application_BeginRequest输出no-cache标头: protected void Application_BeginRequest() { HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1)); HttpContext.Current.Response.Cache.SetValidUntilExpires(false); HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches); HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); HttpContext.Current.Response.Cache.SetNoStore(); } 应用程序在IIS7上运行,模块配置设置为runAllManagedModulesForAllRequests=”true” 。 这意味着所有静态文件也会通过请求管道(并禁用缓存)。 为这些静态文件启用缓存的最佳方法是什么? 在Application_BeginRequest设置响应缓存头之前是否必须检查扩展?还是有更简单的方法(例如完全绕过静态文件的请求管道)?

持久化entity framework查询缓存

我有一个ASP.NET MVC 5 Web应用程序,并使用EF 6.1访问我的数据库。 我有一些相当复杂的LINQ查询,最多需要10秒才能编译,但之后会在几毫秒内执行。 EF确实缓存了这些查询,第二次执行查询时,它会在几毫秒内返回。 但是这个缓存不会持久存在,所以在每个应用程序重新启动时,需要重新编译查询,再次需要10秒。 有没有办法保持这个查询缓存,以便它能够在应用程序重启后幸存下来?

MVCvalidation仅在字符串字段上使RegularExpression成为数字

我在视图模型中有以下属性: [Required] [MaxLength(12)] [MinLength(1)] [RegularExpression(“[^0-9]”, ErrorMessage = “UPRN must be numeric”)] public string Uprn { get; set; } 无论Uprn是一个string ,如果在页面提交的Uprn框中输入了除数字以外的任何内容,我想抛出validation错误。 有了上面的内容,我得到错误“UPRN必须是数字”,无论是字符串还是int 这里发生了什么?

如何修复传递到字典中的模型项的类型是错误的?

我正在尝试运行我的第一个ASP.NET MVC应用程序。 我创建了一个cotroller和视图。 数据来自数据库。 但是,当项目可以运行但是当我尝试浏览客户页面时,我会收到以下错误。 传递到字典中的模型项的类型为’System.Collections.Generic.List`1 [MvcApplication3.Models.Customer]’,但此字典需要类型为“MvcApplication3.Models.Customer”的模型项。 我在这里有点困惑,因为错误说它已经请求了模型类型。 堆栈跟踪是 堆栈跟踪: [InvalidOperationException:传递到字典中的模型项的类型为’System.Collections.Generic.List 1[MvcApplication3.Models.Customer]’, but this dictionary requires a model item of type ‘MvcApplication3.Models.Customer’.] System.Web.Mvc.ViewDataDictionary 1[MvcApplication3.Models.Customer]’, but this dictionary requires a model item of type ‘MvcApplication3.Models.Customer’.] System.Web.Mvc.ViewDataDictionary 1[MvcApplication3.Models.Customer]’, but this dictionary requires a model item of type ‘MvcApplication3.Models.Customer’.] System.Web.Mvc.ViewDataDictionary 1.SetModel(Object value)+585211 System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary)+371 System.Web.Mvc.ViewPage 1.SetViewData(ViewDataDictionary viewData) +48 […]