c# - Asynchronous MVC controller and HttpTaskAsyncHandler -


i'm trying implemented custom httptaskasynchandler custom content management solution. idea route /100/my-little-pony /details/my-little-pony. hope achieve following httptaskasynchandler:

public override async task processrequestasync(httpcontext context) {     try     {         var id = getcontentidfromroutedata();          // retrieve content identified specified id         var contentrepository = new contentrepository();         var content = await contentrepository.getasync(id);          if (content == null)             throw new contentnotfoundexception(id);          // initialize instance of content controller         var factory = controllerbuilder.current.getcontrollerfactory();         var controller = (icontentcontroller) factory.createcontroller(_requestcontext, content.controllername);         if (controller == null)             throw new controllernotfoundexception(content.controllername);          try         {             // retrieve content type values , pass them on the method index pages             var action = _requestcontext.routedata.getrequiredstring("action");             if (action == "index")             {                 contenttype data = null;                 if (controller.contenttype != null)                 {                     data = businesshost.resolve<contenttype>(controller.contenttype);                     data.values = content.parameters.todictionary(p => p.name, p => p.value);                 }                 _requestcontext.routedata.values.add("data", data);             }              var values = _requestcontext.routedata.values;             values.add("name", content.name);             values.add("controllerid", id);             values.add("controller", content.controllername);              controller.execute(_requestcontext);         }                 {             factory.releasecontroller(controller);         }     }     catch (contentnotfoundexception ex)     {         trace.tracewarning($"404: {ex.message}");         _requestcontext.httpcontext.response.statuscode = (int)httpstatuscode.notfound;     } } 

this works wonderfully synchronous requests, when try invoke asynchronous methods ...

@using (html.beginform("save", html.controllerid(), formmethod.post, new { @class = "form-horizontal" })) 

... , being method ...

[httppost] public async task<actionresult> save(newsviewmodel model) { } 

edit i've changed name of method save async isn't inferred, receive new error:

the asynchronous action method 'login' returns task, cannot executed synchronously.

action name saveasync, code refers uses save name. there no magical renaming actions, including async once.

your options:

  • use saveasync refer action
  • use actionname attribute rename action
  • rename method save (but against convention async methods have ...async suffix)

side note: using routing may better option redirects custom handler.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -