Tag: asp.net mvc

如何配置Automapper以自动忽略具有ReadOnly属性的属性?

语境: 假设我有以下“目的地”类: public class Destination { public String WritableProperty { get; set; } public String ReadOnlyProperty { get; set; } } 和一个“source”类,其中一个属性具有ReadOnly属性: public class Source { public String WritableProperty { get; set; } [ReadOnly(true)] public String ReadOnlyProperty { get; set; } } 很明显,但要明确:我将以下列方式从Source类映射到Destination类: Mapper.Map(source, destination); 问题: 有哪些方法可以将Automapper配置为使用ReadOnly(true)属性自动忽略属性? 约束: 我使用Automapper的Profile类进行配置。 我不想弄脏具有Automapper特定属性的类。 我不想为每个只读属性配置Automapper,并且通过这种方式导致大量重复。 可能的(但不适合)解决方案: 1)将属性IgnoreMap添加到属性: [ReadOnly(true)] [IgnoreMap] […]

在异步任务中使用HttpContext

我有以下mvc动作。 public async Task DoSomeLongRunningOperation() { return await Task.Run(() => { //Do a lot of long running stuff //The underlying framework uses the HttpContext.Current.User.Identity.Name so the user is passed on the messagebus. } } 在任务中,HttpContext变为null。 我们做了很多尝试,但没有任何事情让我们确信HttpContext总是在我们的新线程中可用。 有没有在异步任务中使用HttpContext的解决方案? 在我们的Io​​cContainer中,我们注册了以下对象,该对象将用户名传递给框架。 public class HttpContextUserIdentityName : ICredentials { public string Name { get { return HttpContext.Current.User.Identity.Name; } } } […]

如何从VS2012中的简单Nuget安装中解决MVC4 Twitter Bootstrap项目失败?

我在使用Twitter引导程序启动MVC4应用程序时遇到问题。 在VS 2012中,我创建了一个新的空MVC4应用程序,安装了以下软件包: 安装包twitter.bootstrap.mvc4 安装包twitter.bootstrap.mvc4.sample 看起来像一些导航路线选项相关的问题,因为我正在经历这些少数的turorials Link 1和Link 2 。 在阅读有关可能的编译问题的说明时,我重新启动了解决方案,但我收到的错误不是自述文件中的错误,而是集中在NavigationRouteOptions , NavigationRouteFilters , RouteValueDictionary , FilterToken和HasFilterToken : ‘System.Web.Routing.RouteValueDictionary’ does not contain a definition for ‘HasFilterToken’ and no extension method ‘HasFilterToken’ accepting a first argument of type ‘System.Web.Routing.RouteValueDictionary’ could be found (are you missing a using directive or an assembly reference?) …\AdministrationRouteFilter.cs 18 ‘System.Web.Routing.RouteValueDictionary’ does […]

如何在asp.net mvc 2中进行整数模型validation

我有一张注册表,用户必须输入他们房子的面积。 我希望这个值只是一个整数。 有没有办法使用属性asp.net mvcvalidation此值?

dependency injection(使用SimpleInjector)和OAuthAuthorizationServerProvider

对dependency injection的新手,所以这可能是一件简单的事情,但我已经尝试过并且无法弄明白,我正在使用Simple Injector。 我有一个完全使用SimpleInjector的WebApi,现在我想使用OAuth实现安全性。 为此,我开始学习本教程,这非常有用,但不使用Dependancy Injection Token Based Authentication using ASP.NET Web API 2, Owin, and Identity 我的global.asax文件看起来像这样,设置dependency injection(完美工作) protected void Application_Start() { SimpleInjectorConfig.Register(); GlobalConfiguration.Configure(WebApiConfig.Register); } 我创建了一个Startup.Auth.cs文件来配置OAuth public class Startup { public void Configuration(IAppBuilder app) { var OAuthServerOptions = new OAuthAuthorizationServerOptions() { AllowInsecureHttp = true, TokenEndpointPath = new PathString(“/token”), AccessTokenExpireTimeSpan = TimeSpan.FromDays(1), Provider = new […]

ASP.NET MVC – 在Html.ActionLink routeValues中传递模型的麻烦

我的视图如下所示: <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl” %> 我的控制器动作: public ActionResult SortDetails(SearchDataTypeModel model, String sortBy) { model参数为null。 sortBy参数已填充。 我可以将模型中的String属性传递给操作,没有任何问题。 我想传递整个模型。 我有什么想法我做错了吗?

方法上的多个授权属性

我在类方法上指定两个单独的Authorization属性时遇到问题:如果两个属性中的任何一个为true,则允许用户访问。 Athorization类看起来像这样: [AttributeUsage(AttributeTargets.All, AllowMultiple = true)] public class AuthAttribute : AuthorizeAttribute { . . . 和行动: [Auth(Roles = AuthRole.SuperAdministrator)] [Auth(Roles = AuthRole.Administrator, Module = ModuleID.SomeModule)] public ActionResult Index() { return View(GetIndexViewModel()); } 有没有办法解决这个问题,还是需要重新考虑我的方法? 这将在MVC2中运行。

使用HttpContext.Current.Application存储简单数据

我想在我的ASP.NET MVC应用程序中存储一个简单对象(包含三个字符串)的小列表。 该列表是从数据库加载的,并且很少通过编辑站点管理区域中的某些值来更新。 我正在考虑使用HttpContext.Current.Application来存储它。 这样我可以在Global.asax中加载它: protected void Application_Start() { RegisterRoutes(RouteTable.Routes); HttpContext.Current.Application[“myObject”] = loadDataFromSql(); // returns my object } 然后可以根据需要从任何控制器或视图轻松引用它。 然后,如果管理区域调用updateMyObject控制器操作,我可以只更新数据库并再次加载它并替换HttpContext.Current.Application[“myObject”] 。 这样做有什么缺点吗? 看起来它会对我想要实现的目标起作用,但是有没有人知道更好的方法来做到这一点,假设我已经列出的方法有一些主要的缺点?

在MVC 3中创建自定义数据注释validation

例如,我有一个Employee视图模型。 创建员工时,我想validation用户名以确保其不存在。 public class EmployeeViewModel { [ScaffoldColumn(false)] public int EmployeeId { get; set; } [ValidateDuplicate(ErrorMessage = “That username already exists”)] [Required(ErrorMessage = “Username is required”)] [DisplayName(“Username”)] public string Username { get; set; } } 然后让我的ValidateDuplicate函数在某处使用代码来检查重复。 这可能吗?

如何对包含属性的类的List集合进行排序?

我的集合中有一个类列表 List test = new List(); 在我的课程中,我只有一些属性 public string id {get; set;} public DateTime date {get; set;} 现在我通过从2个不同的数据库表中获取一些查询来创建这些类。 然后我从数据库表中获取这2个结果并使用foreach循环我创建一个新的MyClass对象并将其粘贴在我的“test”集合中。 现在,将所有这些类放入列表集合中。 我想对它们进行排序,并通过“date”属性对类进行排序。 我怎么能这样做? 当我从数据库中获取它们时,我无法正确订购,因为我从2个不同的数据库表中获取它们并且单独订购它们只会为每个部分订购它,但是表1可能有12/12/2009因此可能表二。 所以他们需要一起订购。 那么我可以使用linq或其他东西来订购它们吗? 谢谢