Data Annotations for validation, at least one required field?

I have extended Zhaph answer to support grouping of properties. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class AtLeastOnePropertyAttribute : ValidationAttribute { private string[] PropertyList { get; set; } public AtLeastOnePropertyAttribute(params string[] propertyList) { this.PropertyList = propertyList; } //See http://stackoverflow.com/a/1365669 public override object TypeId { get { return this; } } public override bool IsValid(object value) { … Read more

Session state can only be used when enableSessionState is set to true either in a configuration

Did you enable the session state in the section as well? <system.web> <pages enableSessionState=”true” /> </system.web> Or did you add this to the page? <%@Page enableSessionState=”true”> And did you verify that the ASP.NET Session State Manager Service service is running? In your screenshot it isn’t. It’s set to start-up mode Manual which requires you to … Read more

how to gzip content in asp.net MVC?

Here’s what i use (as of this monent in time): using System.IO.Compression; public class CompressAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { var encodingsAccepted = filterContext.HttpContext.Request.Headers[“Accept-Encoding”]; if (string.IsNullOrEmpty(encodingsAccepted)) return; encodingsAccepted = encodingsAccepted.ToLowerInvariant(); var response = filterContext.HttpContext.Response; if (encodingsAccepted.Contains(“deflate”)) { response.AppendHeader(“Content-encoding”, “deflate”); response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress); } else if (encodingsAccepted.Contains(“gzip”)) { response.AppendHeader(“Content-encoding”, “gzip”); … Read more

Adding sub-directory to “View/Shared” folder in ASP.Net MVC and calling the view

Solved this. To add the “Shared/Partials” sub directory I created to the list of locations searched when trying to locate a Partial View in Razor using: @Html.Partial(“{NameOfView}”) First create a view engine with RazorViewEngine as its base class and add your view locations as follows. Again, I wanted to store all of my partial views … Read more

Can’t get sql server compact 3.5 / 4 to work with ASP .NET MVC 2

SQL CE 3.5 does not work with ASP.NET, you must use 4.0 CTP. Download from here. Install the runtime. Copy the following directory contents (including the x86 and amd64 folders) to the bin folder of your ASP.NET app: C:\Program Files\Microsoft SQL Server Compact Edition\v4.0\Private UPDATE: Use System.Data.SqlServerCe.dll from the Desktop folder to avoid Medium Trust … Read more

asp.net MVC: How to redirect a non www to www and vice versa

You might consider a different approach: protected void Application_BeginRequest (object sender, EventArgs e) { if (!Request.Url.Host.StartsWith (“www”) && !Request.Url.IsLoopback) { UriBuilder builder = new UriBuilder (Request.Url); builder.Host = “www.” + Request.Url.Host; Response.Redirect (builder.ToString (), true); } } This will however do a 302 redirect so a little tweak is recommended: protected void Application_BeginRequest (object sender, … Read more

How do I configure ASP.NET MVC routing to hide the controller name on a “home” page?

Try this: private void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”); routes.MapRoute(“default”, “{controller}/{action}/{id}”, new { action = “index”, id = “” }, // Register below the name of all the other controllers new { controller = @”^(account|support)$” }); routes.MapRoute(“home”, “{action}”, new { controller = “device”, action = “index” }); } e.g. /foo If foo is not a controller … Read more