Use specific return types in your ASP.NET MVC action methods (www.bengtbe.com)

published 7 months, 6 days ago, submitted by bengtbebengtbe(450) 7 months, 7 days ago

When looking at ASP.NET MVC examples on the web almost all action methods return ActionResult, even methods that could return a specific subclass...

2 comments | category: | Views: 302

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:

Comments:

posted by robconeryrobconery(160) 7 months, 6 days ago

*** I left this on the post but I think it's worth having it here to. I don't think this is a good idea...

Actually this isn't a very good idea because you can't follow it consistently. Consider handling posts from forms - the general pattern is that if the POST works and the operation is completed you return a RedirectToAction result; if not you return a view. This could happen quite a few times.

I would say for Index/Details actions - this could be more readable - but typing a return is akin to "coupling" if you will and if there was an "IActionResult" I might offer you should *always* use that for the same reasons people use interfaces in programming: Future Proofing.

You don't know that your method will always return a ViewResult - you could change it in the future to return Json (or something cool that MVCContrib is using, or maybe a result type you just created) and boom, you've broken your app.

Code boundaries should be nimble, not strongly typed for the sake of readability :).

posted by bengtbebengtbe(450) 7 months, 6 days ago

Hi Rob.

Thanks for your comment, but I can't say that I agree with your objections. I left a thorough reply on my blog, so if you want to continue the discussion please post it there :)

Bengt

information Login or create an account to comment on this story