views:

93

answers:

3

Hey guys

Just wondering what people think about creating an area to hold/manage json based requests (note I am thinking mostly get data not post data). I know its not your typical use of an area (i.e. normally you would create a different area for blog vs forum) but I am getting to the point where my project isn't huge but I definitely have a a lot of json stuff which seems to be confusing the issue and is making things look "unclean".

For instance, at the bottom of each controller is where I am putting json actions and so that they don't get mixed up with the other actions I prefix them with json - I shouldn't have to do this... Also I have specific view models for json which I have to prefix with json as well... etc, etc.

It would seem much cleaner to have them off in their own area and be able to drop the json prefix all together and have that defined by the area... what do you think or is this a bad idea?

Cheers Anthony

+1  A: 

I think it's a good idea. Having an asynchronous area where all controllers implement only asynchronous actions will certainly clean up your code. The problem will come when your project does become so big you want to expand into regular areas, then you'll end up with some naming conventions that might end up a bit messy.

Odd
At this point I don't think the project will reach that size but I do see your point.
vdh_ant
A: 

You could also just create a separate controller for your json actions. I think this makes more sense than creating an area. Do you need json specific views, content, model etc or just some asynchronous actions?

Victor
I don't have different views but I do have different content and models... its not just being asynchronous.
vdh_ant
A: 

I think that it's not nessesery to create separate area or even separate actions. If the actions return the same data and differ only in the type of the request - ajax or non-ajax you can just check what is the request and use the corresponding data format.

public ActionResult Index()
{
    MyViewModel model = DataAccess.GetMyViewModel(); // Data access code
    if (Request.IsAjaxRequest())
    {
        return Json(model);
    }
    else
    {
        return View(model);
    }
}
Branislav Abadjimarinov
I wish it was this simple but in a lot of cases the model that is returned for the json version is different and generally the context in which its used is not the same either, making the json method semantically different to its get counterpart.
vdh_ant
Then you can check if the method is cassed with ajax and if not return a message that states that this message can be called only with ajax. It is not a good practice to name the methods according to their return type.
Branislav Abadjimarinov