Tag: asp.net mvc 4

基于声明的表单身份validation角色

我正在尝试使用MVC 4中的表单身份validation对用户进行身份validation(我使用的是RavenDB,因此我无法使用标准成员资格提供程序)。 然后,我使用User.IsInRole()方法或AuthorizeAttribute来validation用户是否处于职员角色。 这是我在成功validation时设置票证的位置(目前在UserController.cs ): FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1, model.Email, DateTime.Now, DateTime.Now.AddDays(1), false, model.Email); string hashedTicket = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie( FormsAuthentication.FormsCookieName, hashedTicket); HttpContext.Response.Cookies.Add(cookie); 这是我检查每个请求的票证( Global.asax ): protected void Application_AuthenticateRequest(object sender, EventArgs e) { var authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null) { var authTicket = FormsAuthentication.Decrypt(authCookie.Value); var user = this.UserService.GetUserByEmail(authTicket.Name); […]

使用dropdownlist时Asp.net mvc ModelState的有效性

ModelState.IsValid总是false,因为我在我想提交的表单中使用了一个下拉列表,我得到了这个exception: The parameter conversion from type ‘System.String’ to type ‘System.Web.Mvc.SelectListItem’ failed because no type converter can convert between these types 模型: public class NewEmployeeModel { [Required(ErrorMessage = “*”)] [Display(Name = “Blood Group”)] public IEnumerable BloodGroup { get; set; } } 视图: @Html.LabelFor(m => m.BloodGroup, new { @class = “control-label col-md-3” }) @Html.DropDownListFor(m => m.BloodGroup, Model.BloodGroup, […]

在ASP.NET MVC 4中设置外键

我有2个模型,想要创建一个外键关系。 基本上每个音符都应该分配给用户。 [Table(“UserProfile”)] public class UserProfile { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int UserId { get; set; } public string UserName { get; set; } public string Email { get; set; } } [Table(“UserNotes”)] public class UserNotes { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int NoteId { get; set; } [ForeignKey(“UserProfile”)] public virtual int UserId { get; set; } public […]

iPad上的MVC 4网站速度很慢

我已经开发了一个使用MVC 4的销售点系统。在Windows和Mac上的响应速度和加载时间是即时的,但在iPad上加载页面或执行诸如向篮子添加项目等操作需要8-13秒。 为了提高Web应用程序的速度,我在IIS中启用了压缩并缩小了我的所有java脚本文件,我还使用捆绑将以下.js文件捆绑在一起,这可能会改善页面的加载: jQuery的1.8.2.min.js 淘汰赛2.2.0.js jquery.easing.1.3.js b.popup.min.js(用于显示模式弹出窗口仅6KB) 我在页面上使用的其他javascript文件介于5KB和15KB之间。完成所有这些后,应用程序似乎要快几秒,但仍然需要不可接受的长(8-10秒)。 有没有人在iPad上遇到类似的性能问题,你是如何解决它的?我还能做些什么来提高性能吗? 我正在使用Windows Server 2003和IIS 6.0 这是我的包注册码: public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new ScriptBundle(“~/bundles/jquery”).Include( “~/Scripts/jquery-1.8.2.min.js”, “~/Scripts/jquery.easing.1.3.js”, “~/Scripts/knockout-2.2.0.js”, “~/Scripts/common/common.min.js”, “~/Scripts/popup.min.js” )); bundles.Add(new StyleBundle(“~/Content/css”).Include(“~/Content/site.css”)); BundleTable.EnableOptimizations = true; } 这就是我在母版页上调用的地方: @using System.Configuration Prestige SSC @Scripts.Render(“~/bundles/jquery”) @RenderSection(“scripts”, required: false) @Styles.Render(“~/Content/css”) var screenRefreshTime = ‘@ConfigurationManager.AppSettings[“ScreenRefreshTime”].ToString()’; screenRefreshTime = parseInt(screenRefreshTime); @RenderBody()

Localhost上的ASP.NET MVC Microsoft Live帐户身份validation

我创建了一个空白的新ASP.NET MVC站点。 我在https://account.live.com/developers/上设置了一个应用程序端点,如下所示: API设置: http : //i.imgur.com/bIoV3x9.png 应用程序设置和代码隐藏: http : //i.imgur.com/P3KFyhV.png 我尝试启动我的网站,连接到https://localhost:44300/ ,点击“登录”,然后点击“Microsoft”,我得到一个页面,上面写着: 微软帐户 我们无法完成您的请求 Microsoft帐户遇到技术问题。 请稍后再试。 但它重定向到我的URL是: https://login.live.com/err.srf?lc=1033#error=invalid_request&error_description=The%20provided%20value%20for%20the%20input%20parameter%20’redirect_uri’%20is%20not%20valid。%20The% 20预期%20value%20is%20’https : //login.live.com/oauth20_desktop.srf’%20或%20a%20URL%20which%20匹配%20%20redirect%20URI%20注册%20for%20this%20client%20应用 我相信redirect_uri无效。 期望值是oauth20_desktop.srf的一些URI。 我不知道世界上到底发生了什么/问题是什么。 任何人都可以了解我必须做些什么来测试Microsoft帐户登录到我的localhost运行的MVC站点?

根据地区validation日期

我有一个MVC4应用程序,它在英国编译并托管在美国服务器上。 在我的模型中,我有一个必需的日期字段。 附加到该字段的是日期选择器,在选择日期时,它将日期设置为英国格式“DD / MM / YYYY”,例如2013年12月23日。 在提交表单时,应用程序会抛出validation错误,因为它不期望格式。 在MVC4中我该怎么做: 接受两种日期格式,即覆盖日期格式? 我已设置日期格式,但无法指定多种日期格式 服务器端我使用Linq将日期保存到数据库。 这也会导致错误格式错误。 我怀疑我需要转换到英国约会? 加载详细信息时,请根据用户的位置以正确的格式显示日期 检测用户服务器端的位置? 有没有人知道如何根据用户的全球化进行非常validation? 谢谢

如何将资源字符串组合用于validation属性错误消息?

如果我在validation属性上有错误消息,例如: 名字是必需的 姓氏是必需的 然后像这样的validation属性: [Required(ErrorMessageResourceName = “Error_FirstNameRequired”, ErrorMessageResourceType = typeof(Strings)] public string FirstName {get;set;} 我不想为每个实例做一个翻译。 有没有办法在格式化程序中组合资源字符串,例如: [Required(ErrorMessage = string.Format(“{0} {1}”, Strings.Label_FirstName, Strings.Error_IsRequired))] public string FirstName {get;set;} 当然这不起作用,因为它需要是一个编译时常量。 但是有没有实现这一点,以便我可以构建本地化的字符串并重用已经存在的字符串? 我想过只创建自定义属性,允许设置额外的属性并覆盖默认的输出消息,但这将是太多的重构和kludgy imo。 有任何想法吗?

与区域同名的控制器 – Asp.Net MVC4

我在主/顶部区域有一个Contacts控制器,我有一个名为“Contacts”的区域。 如果我在注册顶级路线之前注册我的区域,我会将POST 404s发送到Contacts控制器: protected void Application_Start() { AreaRegistration.RegisterAllAreas(); ModelBinders.Binders.DefaultBinder = new NullStringBinder(); RouteConfig.RegisterRoutes(RouteTable.Routes); } 而且,如果我在路线后注册我的区域,我的404到联系人控制器就会消失,但我到联系人区域的路线现在是404s。 …记录了许多重复的控制器名称问题,但我没有找到该区域与控制器名称相同的特定方案。 …可能很容易解决。 会很感激帮助。 😀 fwiw,我正在使用显式命名空间注册我的Contacts区域: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”); routes.MapRoute( name: “Default”, url: “{controller}/{action}/{id}”, defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }, namespaces: new[] { “MyMvcApplication.Controllers” } ); }

当前上下文中不存在名称“ConfigureAuth”

当我试图运行我的页面时,我收到错误说, 当前上下文中不存在名称“ConfigureAuth” 在我的Stratup课程中。 我确定已安装所有AspNet Identity库。 接下来我需要做什么,试图解决这个问题? using Microsoft.Owin; using Owin; [assembly: OwinStartupAttribute(typeof(project_name.Startup))] namespace project_name { public partial class Startup { public void Configuration(IAppBuilder app) { ConfigureAuth(app); } } }

User.IsInRole抛出与网络相关或特定于实例的错误

我有这个非常基本的MVC4应用程序通过对象实体模型连接到数据库。 应用程序和数据库之间的连接很好,因为用户能够登录并且Global.asax的以下代码正在工作(它从数据库中成功检索用户角色)。 protected void Application_AuthenticateRequest(object sender, EventArgs args) { if (User != null) { IEnumerable roles = new BusinessLayer.Roles().getAllUserRoles(Context.User.Identity.Name); string[] rolesArray = new string[roles.Count()]; for (int i = 0; i < roles.Count(); i++) { rolesArray[i] = roles.ElementAt(i).RoleName; } GenericPrincipal gp = new GenericPrincipal(Context.User.Identity, rolesArray); Context.User = gp; } } 错误发生在以下行: @if(User.IsInRole(“Administrator”)) { testtttt } 而不是上面,我也尝试了以下但仍然有同样的错误: […]