Problems implementing ValidatingAntiForgeryToken attribute for Web API with MVC 4 RC

You could try reading from the headers: var headers = actionContext.Request.Headers; var cookie = headers .GetCookies() .Select(c => c[AntiForgeryConfig.CookieName]) .FirstOrDefault(); var rvt = headers.GetValues(“__RequestVerificationToken”).FirstOrDefault(); AntiForgery.Validate(cookie != null ? cookie.Value : null, rvt); Note: GetCookies is an extension method that exists in the class HttpRequestHeadersExtensions which is part of System.Net.Http.Formatting.dll. It will most likely exist in … Read more

Automatic Migrations for ASP.NET SimpleMembershipProvider

update-database -verbose doesn’t work because your model has been changed after your data table already existed. First, make sure there are no changes to the UserProfile class. Then, run: Add-Migration InitialMigrations -IgnoreChanges This should generate a blank “InitialMigration” file. Now, add any desired changes to the UserProfile class. Once changes are added, run the update … Read more

What effect does the new precompile during publishing option have on MVC4 applications?

Using the ASP.NET precompiler can have the following impact on your MVC app: If you have anything in App_Code, it will be precompiled into a DLL before deployment. Without precompiling, this would happen on the fly by the ASP.NET runtime. If you choose the option to not make your pages updateable (i.e. uncheck the first … Read more

where should i place the js script files in a mvc application so jquery works well?

In your layout file, the script tag to load jQuery library is included at the end of the page. But there is another section called scripts below that. So in your individual pages ( Ex : Index,View etc..) You should be putting your javascript inside the section scripts <body> @RenderBody() @Scripts.Render(“~/bundles/jquery”) @RenderSection(“scripts”, required: false) </body> … Read more

WebApi Json.NET custom date handling

Change your setting set up code like this: JsonMediaTypeFormatter jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter; JsonSerializerSettings jSettings = new Newtonsoft.Json.JsonSerializerSettings() { Formatting = Formatting.Indented, DateTimeZoneHandling = DateTimeZoneHandling.Utc }; jSettings.Converters.Add(new MyDateTimeConvertor()); jsonFormatter.SerializerSettings = jSettings; In your code you are just changing local variable value.

MVC 4 date culture issue?

Try adding this attribute to your MyDate property: [DisplayFormat(DataFormatString = “{0:dd/MM/yyyy}”, ApplyFormatInEditMode = true)] Though setting the culture in the web.config should do it, this should force it into that format. UPDATE Ok, so the above answer doesn’t really solve the issue, though it is important if you do want to change the format of … Read more

MVC 4 Beta side by side installation error

After installing MVC4 beta today, a few of my MVC 3 projects would not compile. (ModelClientValidationRule conflict) The fix was: Edit: ProjectName.csproj Change <Reference Include=”System.Web.WebPages”/> To <Reference Include=”System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL “/>

ASP.NET MVC 4 custom Authorize attribute – How to redirect unauthorized users to error page? [duplicate]

You have to override the HandleUnauthorizedRequest as specified here. public class CustomAuthorize: AuthorizeAttribute { protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)     {         if(!filterContext.HttpContext.User.Identity.IsAuthenticated)         {             base.HandleUnauthorizedRequest(filterContext);         }         else         {             filterContext.Result = new RedirectToRouteResult(new             RouteValueDictionary(new{ controller = “Error”, action = “AccessDenied” }));         }     } } **Note: updated conditional statement Jan ’16