Posts Tagged ‘MVC 4’

ASP.NET MVC 4 RC: Get current controller name from HttpContext

To find out the name of the current controller from a HTTP context, you can use the following code snippet: var controllerName = RouteTable.Routes.GetRouteData( new HttpContextWrapper(HttpContext.Current) ).Values["controller"].ToString();var controllerName = RouteTable.Routes.GetRouteData( new HttpContextWrapper(HttpContext.Current) ).Values["controller"].ToString();

Read more…

ASP.NET MVC 4 RC & ASP.NET Web API: Implementing custom XmlMediaTypeFormatter that ignores XML namespaces

In this blog post I will show how to implement a custom XmlMediaTypeFormatter that extends the default ASP.NET Web API XmlMediaTypeFormatter in a way that it ignores XML namespaces when parsing xml messages. By default the ASP.NET Web API XmlMediaTypeFormatter is not able to parse XML requests that contain any XML namespace declarations. If you […]

Read more…

ASP.NET MVC 4 RC & ASP.NET Web API: Return “HTTP Status code 4xx” instead of throwing InvalidOperationException “No MediaTypeFormatter is available to read an object of type ‘[…]’ from content with media type ‘[…]'”

By default ASP.NET Web API (included in ASP.NET MVC 4) throws the following exception when trying to process a request with an unsupported content-type (like “text/plain”): System.InvalidOperationException No MediaTypeFormatter is available to read an object of type ‘[…]’ from content with media type ‘[…]’. IMHO, sending a wrong content-type is a client-side error, and the […]

Read more…