Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the replacement of Controller.ReadFromRequest in ASP.NET MVC?

Tags:

I am attempting to update a project from ASP.NET MVC Preview 3 to Preview 5 and it seems that Controller.ReadFromRequest(string key) has been removed from the Controller class. Does anyone know of any alternatives to retrieving information based on an identifier from a form?

like image 515
Kevin Faustino Avatar asked Aug 30 '08 15:08

Kevin Faustino


People also ask

What is request handler in MVC?

MVC Request Handler (MVCRouteHandler) MVC handler is responsible for initiating MVC applications. The MvcRouteHandler object creates an instance of the MvcHandler and passes the instance of RequestContext to MvcHandler.

What is request and response in MVC?

Handlers are responsible for generating the actual response in MVC. They implement the IHttpHandler class and only one handler will execute per request. On the other hand, HttpModules are created in response to life cycle events. Modules can, for example, be used for populating HttpContext objects.

Which is a new instance created for every HTTP request?

A Controller is created for every request by the ControllerFactory (which by default is the DefaultControllerFactory ).


2 Answers

Looks like they've added controller.UpdateModel to address this issue, signature is:

UpdateModel(object model, string[] keys)

I haven't upgraded my app personally, so I'm not sure of the actual usage. I'll be interested to find out about this myself, as I'm using controller.ReadFromRequest as well.

like image 188
matt Avatar answered Sep 24 '22 05:09

matt


Not sure where it went. You could roll your own extension though:

public static class MyBindingExtensions {

public static T ReadFromRequest < T > (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;
}

}
like image 42
Dane O'Connor Avatar answered Sep 21 '22 05:09

Dane O'Connor