Replacement of Controller.ReadFromRequest in ASP.NET MVC
I was attempting to update a project from old version of MVC to new version of ASP.NET MVC and it seems that Controller.ReadFromRequest(string key) has been removed from the Controller class.
Upon doing some research in Google, I came across an extension method which works similar to old ReadFromRequest method. The extension method is:
public static class MyBindingExtensions
{
public static T ReadFromRequest(this Controller controller, string key)
{
// Setup
HttpContextBase context = controller.ControllerContext.HttpContext;
object val = null;
T result = default(T);
// Gaurd
if (context == null)
return result; // no point checking request
// Bind value (check form then query string)
if (context.Request.Form[key] != null)
val = context.Request.Form[key];
if (val == null)
{
if (context.Request.QueryString[key] != null)
val = context.Request.QueryString[key];
}
// Cast value
if (val != null)
result = (T)val;
return result;
}
}
I was attempting to update a project from old version of MVC to new version of ASP.NET MVC and it seems that Controller.ReadFromRequest(string key) has been removed from the Controller class.
Upon doing some research in Google, I came across an extension method which works similar to old ReadFromRequest method. The extension method is:
public static class MyBindingExtensions
{
public static T ReadFromRequest
{
// Setup
HttpContextBase context = controller.ControllerContext.HttpContext;
object val = null;
T result = default(T);
// Gaurd
if (context == null)
return result; // no point checking request
// Bind value (check form then query string)
if (context.Request.Form[key] != null)
val = context.Request.Form[key];
if (val == null)
{
if (context.Request.QueryString[key] != null)
val = context.Request.QueryString[key];
}
// Cast value
if (val != null)
result = (T)val;
return result;
}
}
No comments:
Post a Comment