I want to be able to check the form inputs prior to launching a long running asynchronous task.
Two approaches that come to mind:
Check values on the Begin method and
throw an exception?
Post to a normal (synchronous method) which validates as per normal. redirects to the asynchonrous method if no errors found.
Throwing an exception i thought would be a simple solution. I can't return the view from the begin method, so the exception is handled on the end method. Only it isn't getting across to the end method (I thought this was the normal pattern)
Validating in a normal synchronous method is fine... but how do i transfer or redirect the request to the asynchronous method???
You don't need to use exceptions or a synchronous method, you can just pass back a different IAsyncResult (assuming that's the pattern you're using - if you're using the event or delegate patterns, you'd still be able to achieve something similar without exceptions).
Here's a simple example of this where we use a dummy delegate to return if there's an error (in this case an invalid ID):
public class MyAsyncController : AsyncController
{
public IAsyncResult BeginFoo(int id, AsyncCallback callback, object state)
{
Action errorDelegate = () => ViewData["errors"] = "Invalid ID";
// Here's our validation check, return the error delegate if necessary
if (id <= 0) return errorDelegate.BeginInvoke(callback, state);
var webRequest = WebRequest.Create("http://www.apple.com");
return webRequest.BeginGetResponse(callback, webRequest);
}
public ActionResult EndFoo(IAsyncResult asyncResult)
{
if (asyncResult.AsyncState is WebRequest)
{
var webRequest = (WebRequest) asyncResult.AsyncState;
var httpResponse = (HttpWebResponse) webRequest.EndGetResponse(asyncResult);
ViewData["status"] = httpResponse.StatusCode;
}
return View();
}
}
Related
I am using Edge.js so that I can call Node.js from C#. According to the documentation in the link I would do that similar to the following:
[HttpPost]
public ActionResult Input(InputModel obj)
{
validateInput(obj);
return View();
}
private async void validateInput(object obj)
{
var func = Edge.Func(#"
return function (data, callback){
var username = data.username,
email = data.email;
callback(null, username);
}
");
ViewBag.Msg = (string)await func(obj);
}
However, I get the following run time error:
Additional information: An asynchronous operation cannot be started at this time.
Asynchronous operations may only be started within an asynchronous handler or module or during
certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the
Page is marked <%# Page Async="true" %>. This exception may also indicate an attempt to call an
"async void" method, which is generally unsupported within ASP.NET request processing. Instead,
the asynchronous method should return a Task, and the caller should await it.
My question is two-fold:
1.How do I make the page, async=true. I know how to do this for a web forms project but not a MVC project.
2.Is there a better way to do what I am trying to do? A red flag will probably go up when you see that I am returning void however this is do to the fact that Edge.js is being used. Even so, I have tried returning a Task and then task.Wait() in the calling method but the task never finishes.
After trying some different things, the following solution worked for me.
Even though I answered my own question, and it seems trivial, I am not removing this question as there are not a lot of knowledge on the web about Edge.js.
[HttpPost]
public async Task<ActionResult> Input(InputModel obj)
{
ViewBag.Msg = await validateInput(obj);
return View();
}
private async Task<string> validateInput(object obj)
{
var func = Edge.Func(#"
return function (data, callback){
var username = data.username,
email = data.email;
callback(null, username);
}
");
return (string)await func(obj);
}
I open Database connection in my controller and pass the opened connection to my Model for using that connection.
but since 2nd time calling to the controller(Ajax), it return an error message that say 'Connection must be open.'
I reloaded page, and call the controller again then it works well. but for the 2nd time calling, it keep return that error message.
My controller has the code to open DB Connection, so I believed, it should open database for every processing(Calling). and the (open) code is in using{} block so after using it, it should be closed automatically.
but I think I'm doing wrong :(
here is my code,
public JsonResult myControllerMethod() // Action Controller
{
using (AdsConnection conn = new AdsConnection(connString))
{
conn.Open();
AdsTransaction txn = conn.BeginTransaction(); // Begin Transaction
try
{
MyModel mo = new MyModel();
mo.doProcess(conn); // pass connection to model
txn.Commit();
return Json(new { success = true });
}
catch (Exception e)
{
txn.Rollback();
return Json(new { success = false, message = e.Message });
}
}
}
MyModel
public void doProcess(AdsConncection conn){ // Model
try{
..
//AdsCommand select, update, delete and insert....
..
}catch(Exception e){
throw e;
}
}
Anybody know, what I am doing wrong? please advice me.
Thanks
You shouldn't pass a database connection to your view - your view should accept an object (as a model) that contains "solid" data. A view should have no logic of its own, and a Model's logic should only be concerned with modifying internal state, such as performing validation (not verification) or some internal data transformation. The task of populating a Model from a database should be done by the controller or a separate mapping class (or by some kind of "Populate" method of the model, but this method should be called and contained within the controller's action method).
That said, the problem you're experiencing is because you're using using() blocks. A using() block will always call the IDisposable.Dispose() method of its subject object, in this case a DB connection. SqlConnection.Dispose calls its .Close method, which is why the connection object is closed whenever the method returns.
I have an action like shown below. In GetAvailableBookList, I get the list and if there is not any available book redirect to a message page. But in action part code continues to execute and gets an exception and I find myself in error page.
I don't want to use return RedirectToAction or something like that because there are a lot of places where we use this redirect logic in our application.
public ActionResult ActionName()
{
List<BookType> bookList = GetAvailableBookList();
// some code
return View("RelatedView");
}
private List<BookType> GetAvailableBookList()
{
....
list = GetList();
if(list.Count == 0)
{
System.Web.HttpContext.Current.Response.Redirect(messagePageUrl, true);
}
else return list;
}
Unfortunately, Response.Redirect() isn't really friendly with ASP.NET MVC. My rule of thumb is if it comes from HttpContext I don't want to touch it in the controller (of course there are many exceptions to that rule) -- especially since it improves testability.
My suggestion is to use RedirectToAction, but since you don't want to repeat code you can do it in such a way that you don't have to repeat code (although in this case I don't see a problem with repeating code).
public ActionResult LoadBookListAndContinue(
Func<List<BookType>, ActionResult> continuation)
{
var list = LoadBooklist();
if(list.Any())
{
return action(continuation);
}
return new RedirectResult(messagePageUrl);
}
// in your controller
public ActionResult ActionName()
{
return LoadBookListAndContinue(
list => {
// some code
return View("RelatedView");
});
}
Is it pretty? No, but it works better than the Redirect exception.
Use
return RedirectToAction("NoListAvailable");
if you have a specific action you would like to execute. The NoListAvailable action can return a view indicating the problem.
Alternatively, you could return the view directly
return View("NoListAvailable");
The exception you are getting is probably ThreadAbortException and this is something you cannot avoid unless you allow the thread to continue (2nd argument in Response.Redirect).
On a side note your current solution is generally flawed. You should use RedirectToAction in each action when your method returns an empty list.
Throwing a specific exception and redirect where you catch it may be solution
Try to write
System.Web.HttpContext.Current.Response.Redirect(messagePageUrl, false);
I have a controller action that does some work in the database and then exits when it's finished. This action is being called via jQuery's ajax function with the dataType set to 'json'.
If I set the return type of the action to void, everything will function just fine except Firefox will show an error in the console that says: "no element found".
It makes sense that Firefox would throw this error if it was expecting XML to come back. However, even when I change the dataType property of the ajax call to "text", I still receive the error. In order to get rid of the error with the return type void, I would have to set the Response's ContentType to "text/html". Or I could set the return type to JsonResult and return a new [empty] JsonResult object.
I'm sure there are several ways I can make this error go away, but I wanted to know the proper way to handle actions with no return values being called via ajax.
If it matters, I'm also using the async controller action pattern.
public void DoSomethingAsync(SomeJsonObjectForModelBinding model)
{
// do some database things
}
public void DoSomethingCompleted()
{
// nothing to do...
// what should my return type be?
// do I need to set the content type here?
}
I know this doesn't exactly answer your question, but I would argue that you should always have a return value coming back from an AJAX or web service call. Even if only to tell you that the operation was successful, or otherwise return the error (message) back to you.
I often define a class like this:
public class JsonResultData
{
private bool _success = true;
public bool Success
{
get { return _success; }
set { _success = value; }
}
public object Value { get; set; }
public List<string> Errors { get; set; }
public JsonResultData()
{
this.Errors = new List<string>();
}
}
And then use it to return data or any other call meta data in the JsonResultData wrapper like so:
return new JsonResult {
Data = new JsonResultData { Value = returnValue, Success = true }
};
I can't comment because of my reputation but I still wanted to contribute to clear the confusion in Kon's answer.
In an application I caught all exceptions within an ActionMethod, set an HttpStatusCode and added an error message to the response. I extracted the message in the Ajax error function and showed it to the user.
Everything worked out fine until the application got put on the staging server, who had some kind of settings that did not allow a return message within an erroneous response. Instead some standard Html was transmitted resulting in a JS error processing the response.
In the end I had to rewrite all my exception handling returning my application errors as successful Ajax call (which it actually is) and then differ within the Ajax success function, just the way it should be.
You should not mix system-level and application-level feedback. You may not be able to control the system-level feedback the way your application needs.
I'm trying to use async from the asp.net mvc futures, using my own async delegate. Haven't figured out how to make it work. Here's the code:
public delegate String GetString();
public String getHello() { return "Hello"; }
public IAsyncResult BeginHello(AsyncCallback cb, Object state)
{
GetString dlgt = getHello;
return dlgt.BeginInvoke(cb, state);
}
public ActionResult EndHello(IAsyncResult asyncResult)
{
return View();
}
In EndHello, asyncResult.IsCompleted=True, but asyncResult.AsyncState==null. I expected to have AsyncState=="Hello".
What am I missing?
Also, does it even make sense to arrange it this way? Or does this cause it to use the same thread pool anyway? Basically my thought was to put a datareader in my asynchronous function, thinking that I could loop through the reader populating a collection of objects and only return when they're done. Is it better to use BeginExecuteReader and populate the objects on the main thread?
EDIT: To anyone reading this later, this is in fact the wrong way to do it, and won't help you at all. BeginExecuteReader is the way to go.
You will not get Hello in AsyncState.
Hello will be returned when you do an EndInvoke().
e.g.
Here's the code you should be using:
public String EndHello(IAsyncResult asyncResult)
{
GetString getStringDel = (asyncResult as AsyncResult).AsyncDelegate as GetString;
return getStringDel.EndInvoke(asyncResult);
}
You can refer to:
MSDN link Calling Synchronous Methods Asynchronously
SO answer to What is AsyncCallback?