Web Api Request Content is empty in action filter

The request body is a non-rewindable stream; it can be read only once. The formatter has already read the stream and populated the model. We’re not able to read the stream again in the action filter. You could try: public class LogAttribute : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext) { var myModel = actionContext.ActionArguments[“myModel”]; … Read more

Dependency injection and ASP.Net Membership Providers

If you are configuring the custom membership providers via the <membership> element in the Web.config file, then I can see the issues you will have with dependency injection. The providers are constructed and managed by the framework, and there is no opportunity for you to intercept that construction to provide additional dependency injection for the … Read more

ASP.net MVC support for URL’s with hyphens

C# version of John’s Post for anyone who would prefer it: C# and VB version on my blog public class HyphenatedRouteHandler : MvcRouteHandler{ protected override IHttpHandler GetHttpHandler(RequestContext requestContext) { requestContext.RouteData.Values[“controller”] = requestContext.RouteData.Values[“controller”].ToString().Replace(“-“, “_”); requestContext.RouteData.Values[“action”] = requestContext.RouteData.Values[“action”].ToString().Replace(“-“, “_”); return base.GetHttpHandler(requestContext); } } …and the new route: routes.Add( new Route(“{controller}/{action}/{id}”, new RouteValueDictionary( new { controller = “Default”, … Read more

AJAX Post of JavaScript String Array to JsonResult as List Always Returns Null?

I faced the same problem after updating to jquery 1.4.2. You can find the solution here (in the Ajax section). Adding traditional : true in the ajax options should work. $.ajax({ type: “POST”, traditional: true, url: “/Test/JSONTestAction”, async: false, data: parms, dataType: “json”, success: function(data) { // success } });

401 response code for json requests with ASP.NET MVC

In classic ASP.NET you get a 401 http response code when calling a WebMethod with Ajax. I hope they’ll change it in future versions of ASP.NET MVC. Right now I’m using this hack: protected void Application_EndRequest() { if (Context.Response.StatusCode == 302 && Context.Request.Headers[“X-Requested-With”] == “XMLHttpRequest”) { Context.Response.Clear(); Context.Response.StatusCode = 401; } }

LINQ to Entities does not recognize the method ‘System.String ToString()’ method in MVC 4

You got this error because Entity Framework does not know how to execute .ToString() method in sql. So you should load the data by using ToList and then translate into SelectListItem as: var query = dba.blob.ToList().Select(c => new SelectListItem { Value = c.id.ToString(), Text = c.name_company, Selected = c.id.Equals(3) }); Edit: To make it more … Read more

Html.BeginForm() with an absolute URL?

BeginForm method has several overloads. In order to set the action attribute on the form tag with the desired url, you need to use following overload of BeginForm: BeginForm(String, String, FormMethod, IDictionary<String, Object>) // here are the parameter names: BeginForm(actionName, controllerName, method, htmlAttributes) Since you want to post to an external site, there is no … Read more