One MVC3 Controller method two different ActionResult types?
Currently I have a Controller, AssetController, that has methods for navigating a tree of assets.
I use this controller to navigate a "gallery" of assets and the controller methods currently return ViewResult
.
I am starting to build another way of viewing this same data which will make use of ajax and will not want ViewResult
returned but rather JsonResult
.
I would like to re-use as much of my controller logic as possible. I can think of two options here:
Change all my controller methods to return only
JsonResult
then change my current View to use the json to display the full page.Drawbacks: This pushes more page loading / redirecting logic onto the client. It also make deep linking a problem I think. With full views the URL's of pages are very "link friendly" in that a user can copy and paste a link and pass it to someone and it will go where they expect. Returning JsonResults mean the URL doesn't change so deep linking becomes more challenging
Refactor my current controller so that "shared" code is moved into private methods in the controller. Create new actions that return
JsonResult
and keep existing methods that return ViewResult.This is currently my choice as it seems to keep more code in the controller which makes unit testing it easier. But it also seems to be a little too coupled to the UI, what happens when I want to return the data in different formats, do I just keep adding new controller actions with different 开发者_Go百科
ActionResult
types? Is that so bad?
Is there a smarter way to handle situation or is one of my approaches more favorable than the other?
UPDATE
Here is some code examples to clarify:
Currently I have this:
public ActionResult ViewObject(PathModel inPath)
{
using (engine)
{
var metaData = engine.GetFileMetaData(inPath.Path);
return View(metaData); //This is returning a ViewResult type
}
}
I am thinking about doing something like this
private FileMetaDataModel ViewObjectInternal(PathModel inPath)
{
using (engine)
{
var metaData = engine.GetFileMetaData(inPath.Path);
return metaData;
}
}
public ActionResult ViewObjectAsJSON(PathModel inPath)
{
var jsonResult = new JsonResult();
jsonResult.Data = ViewObjectInternal(inPath).ToJson();
return jsonResult // this is returning my data as JSON;
}
public ActionResult ViewObjectAsView(PathModel inPath)
{
return View(engine.GetFileMetaData(inPath.Path)); //This is returning a ViewResult type
}
Why not just simply return ActionResult?
public ActionResult Detail()
{
// controller logic here
if (Request.IsAjaxRequest())
return Json(model);
return View(model);
}
well you can set result type as ActionResult. All other results are inherited from it
Option #3 could be use a mix of both. You don't need to use jsonresult 100% of the time. If you are worried though about the urls, you could use a mix of the both. You have your main view which then dynamically loads each result with jQuery. This is often a better fit for applications with multiple pieces of data - so the perception is the app is loading faster (main view returns followed by bound data) - so you could surely go that route as well understanding that search engines won't index your returned data. Im assuming here your applications data though wouldnt be indexed. If it will - then jsonresult is not the way to go here.
精彩评论