Tassadaque
Tassadaque

Reputation: 8199

separate controller for ajax actionresults in asp.net mvc 3

For ajax calls actionresult I am thinking of two options

  1. Place the actionresults in controller most suited to their functions
  2. Make a separate controller for ajax calls

In second approach i may use separate files to functionally divide the controller and use partial ajaxcontroller class in each file. Second option may be helpful if there are some actionfilter attributes that i want to apply on actionresults other than the ajax actionresults e.g Authorize attribute may be applied to normal action result. I may use Ajaxonly attribute on ajax actionresults to secure them
which option would u prefer

Upvotes: 3

Views: 1110

Answers (1)

jgauffin
jgauffin

Reputation: 101150

Why do you want to do that? For me, a controller is tied to a certain model, not a certain type of output format.

public ActionResult Users()
{
    var users = _repository.Find(); 
    var viewModel  = Mapper.Map(users); // automapper or similar
    return Request.IsAjax() ? Json(viewModel) : View(viewModel);
}

To answer your update

It's better to create a CustomAuthorizeAttribute that checks if it's a ajax request or a regular request and do the proper authorization. Your controllers should not be aware of how authorization is made.

Upvotes: 5

Related Questions