ASP.NET MVC Support?

Aug 11, 2009 at 11:27 AM

Hi guys,

Has anyone gotten the Toolkit to run in MVC ? Are there any plans to officially support this in MVC?

Cheers

Beyers Cronje

Aug 13, 2009 at 9:49 AM

Please vote on http://facebooktoolkit.codeplex.com/WorkItem/View.aspx?WorkItemId=13718

Official MVC support in the toolkit would be a great thing to have.

Sep 2, 2009 at 12:48 PM
Edited Sep 2, 2009 at 12:51 PM

I found a new Codeplex project that implements a MVC addon that is compatible with FDT 2.1 http://facebooktoolkitmvc.codeplex.com/

Maybe the developers of FDT can look at merging this addon into mainline FDT ?

Beyers

PS I managed to get the above addon to work with the DEV branch (which I presume will be the base for FDT 3.0 ?), let me know should anyone want more info on this.

Sep 9, 2009 at 12:39 PM

I am starting a new FB project and would like to use the MVC pattern. Please post more info on how you integrated the add on.

Thanks!

Sep 10, 2009 at 1:25 PM

Appreciate that someone has done some work to get MVC support, but i wouldn't recommend merging or using the MVC addon out there. It seems a bit like a hack, as you have to pass the non-strongly typed fbApi object around for each function in your controller and views etc.

I do like how http://facebook.codeplex.com/ has added MVC support though so maybe can get some ideas from there. eg. the fbApi can be referenced by properties on the controller and views.

Hopefully someone from the FDT dev team can work on integrating the MVC support properly. I think they are best versed on how to do it best.

Sep 11, 2009 at 12:27 AM
Edited Sep 11, 2009 at 12:30 AM
thetrainer wrote:

Appreciate that someone has done some work to get MVC support, but i wouldn't recommend merging or using the MVC addon out there. It seems a bit like a hack, as you have to pass the non-strongly typed fbApi object around for each function in your controller and views etc.

I do like how http://facebook.codeplex.com/ has added MVC support though so maybe can get some ideas from there. eg. the fbApi can be referenced by properties on the controller and views.

Hopefully someone from the FDT dev team can work on integrating the MVC support properly. I think they are best versed on how to do it best.

I have to agree that the addon is far from elegant and that "official" MVC integration from the FDT team would be ideal. But since they (the FDT team) has not given any indication or roadmap for this I am willing to use the addon in the mean time. It does work right now and with proper abstraction in my app shouldn't be too hard to port to an official FDT MVC implementation in the future. I'm not sure about the non-strongly typed object you are refering to since the addon ActionFilterAttribute implementation passes a strongly typed facebook.API object to my controller action methods and using ViewModel pattern extends this to my Views as well.

http://facebook.codeplex.com/ is definately an option and offers a much cleaner and well thought out integration with the MVC framework. Personally however I feel FDT's implementation of the Facebook API is far more mature, has a much larger following, has some connection with MS, and updates seem to trickle in a lot more frequent as of late. While http://facebook.codeplex.com/ seems to be very quiet lately (last checking was in May), there are small anoying unresolved bugs I had to manually fix which should have been committed to trunk already http://facebook.codeplex.com/Thread/View.aspx?ThreadId=63256 ,  and the discussion forum is also very quiet.

I would definately recommend the FDT team have a look at the MVC implementation of http://facebook.codeplex.com/ though as it seems well thought out and designed.

 

 

 

Coordinator
Sep 15, 2009 at 4:27 PM

I am looking into this this week.

 

I will take a look at how it is working on facebook.codeplex.com.  Version 3.0 is nearing release.  (Probably next week).  I'd love to get some solid mvc support in for that release.  This is one of my top priorities for this week.  Will keep everyone up to date.

Sep 15, 2009 at 4:51 PM
rpowers119 wrote:

I am looking into this this week.

 

I will take a look at how it is working on facebook.codeplex.com.  Version 3.0 is nearing release.  (Probably next week).  I'd love to get some solid mvc support in for that release.  This is one of my top priorities for this week.  Will keep everyone up to date.

Great news, two thumbs up for both v3.0 and MVC support!

Sep 18, 2009 at 12:21 AM

rpowers and I talked this over during lunch today and here is what we are thinking.

 

1. We add an attribute for controller actions to handle authentication

2. We add an extension method to the controller to get an instance of the api object to make calls to facebook. 

 

The code would look like this:

 

        [FacebookAuthorization(IsFbml = true, ApiKey = APIKEY, Secret = SECRET)]
        public ActionResult Index()
        {
            Api api = this.GetApi(APIKEY, SECRET);
            var user = api.Users.GetLoggedInUser();
            return Content("Hello, " + user.ToString());
        }

public class HomeController : Controller

{

private const string APIKEY = "xxxxxxx";

        private const string SECRET = "yyyyyyy";

 

        [FacebookAuthorization(IsFbml = true, ApiKey = APIKEY, Secret = SECRET)]

        public ActionResult Index()

        {

            Api api = this.GetApi(APIKEY, SECRET);

            var user = api.Users.GetLoggedInUser();

            return Content("Hello, " + user.ToString());

        }

}

(Note: you don't have to set the api key / secret if they are in the web.config)

The FacebookAuthorization attribute handles authenticating to facebook. So if a user is not logged in then the actionfilter intercepts the request, redirects to facebook and then your method is called after the user logs in. We added the extension method to get the api object because we needed some way to attach it for use in your code.  We talked about making base controllers to do this, but that didn't seem like the best approach.  I like this approach because it seems the least invasive to the rest of your code. 

Actually I just looked at that code someone posted above. I guess our approach is pretty similar except you don't have to put the api object in any of the controllers' parameters.

Anyone dislike this approach? If so, how would like to see it work?

 

Sep 18, 2009 at 1:12 AM

That would work for me. It keeps the MVC framework "clean" and you have easy access to the Api.

Will the 'P3P: CP="CAO PSA OUR"' header be added inside the action filter?

Beyers

Sep 18, 2009 at 3:17 AM

I hadn't thought about that. I think we can just add it to the ResultExcuted on the filter if it's an iframe. Thanks for reminding me.

Sep 18, 2009 at 5:03 AM

Ok rpowers just checked in the latest with some mvc support. We are probably going to add something to the attribute to support additional permissions that you can request during the authorization, but please try it out if you can. We didn't really do extensive testing yet, so it would be great if some people could help test it out. To get started just create a new MVC project and reference the facebook and facebook.web.mvc projects or dlls. In the default project you could just replace your home controller with:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using Facebook;

using Facebook.Rest;

using Facebook.Web.Mvc;

 

namespace MvcApplication1.Controllers

{

    [HandleError]

    public class HomeController : Controller

    {

        private const string APIKEY = "yourkey";

        private const string SECRET = "yoursecret";

 

        [FacebookAuthorization(IsFbml = true, ApiKey = APIKEY, Secret = SECRET)]

        public ActionResult Index()

        {

            Api api = this.GetApi(APIKEY, SECRET);

            var user = api.Users.GetLoggedInUser();

            return Content("Hello, " + user.ToString());

        }

 

        public ActionResult About()

        {

            return View();

        }

    }

}

 

That is for FMBL. For Iframe apps just change the attribute to [FacebookAuthorization(IsFbml = false, ApiKey = APIKEY, Secret = SECRET)]  You could also leave the ApiKey and Secret out of the attribute and call to GetApi() if you put them in the web.config as usual.

Any feeback would be appreciated. 

Sep 18, 2009 at 9:17 AM

Great, I'll test it over the weekend.

Beyers

 

Sep 21, 2009 at 12:00 AM

Hi kmarshall,

I took some time to test and so far it seems to work nicely.

A couple of things:

1) GetApi() controller extension always makes use of FBMLCanvasSession. For what I tested this was not a problem in my IFrame app, but personally I think using a IsFbml parameter like the action filter will probably be safer in the long run.

2) If I go directly to my canvas page URL (e.g. http://apps.facebook.com/myapp ) before I've been authenticated, FDT redirects me to the FB login page. Once I've been authenticated FB then redirects me to my canvas callback URL (e.g. http://myapp.mydomain.com ) outside FB instead of my canvas page URL.

This happens because currently FDT 3.0 checks if the query parameter "fb_sig_in_canvas"is present:

 

protected string GetLoginUrl()
{
	string canvasParam = HttpContext.Current.Request[QueryParameters.InCanvas] == "1" ? "&canvas" : string.Empty;
	return string.Format("http://www.facebook.com/login.php?api_key={0}&v=1.0{1}", ApplicationKey, canvasParam);
}

 

Are there any scenarios where a CanvasSession will legitimately not be in canvas during login redirection? My current workaround is just to always include "&canvas" for IFrameCanvasSession redirects, but I'm not sure what FB integration features I'm breaking through this :)

Beyers

 

 

Sep 21, 2009 at 8:09 PM

Thanks for testing it out. That's good feedback. I'll look into both those issues. i think we'll just access the isfbml param on the controller so you don't have to pass anything in.

Nov 12, 2009 at 1:59 PM
kmarshall wrote:

Ok rpowers just checked in the latest with some mvc support. We are probably going to add something to the attribute to support additional permissions that you can request during the authorization, but please try it out if you can. We didn't really do extensive testing yet, so it would be great if some people could help test it out. To get started just create a new MVC project and reference the facebook and facebook.web.mvc projects or dlls. In the default project you could just replace your home controller with:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using Facebook;

using Facebook.Rest;

using Facebook.Web.Mvc;

 

namespace MvcApplication1.Controllers

{

    [HandleError]

    public class HomeController : Controller

    {

        private const string APIKEY = "yourkey";

        private const string SECRET = "yoursecret";

 

        [FacebookAuthorization(IsFbml = true, ApiKey = APIKEY, Secret = SECRET)]

        public ActionResult Index()

        {

            Api api = this.GetApi(APIKEY, SECRET);

            var user = api.Users.GetLoggedInUser();

            return Content("Hello, " + user.ToString());

        }

 

        public ActionResult About()

        {

            return View();

        }

    }

}

 

That is for FMBL. For Iframe apps just change the attribute to [FacebookAuthorization(IsFbml = false, ApiKey = APIKEY, Secret = SECRET)]  You could also leave the ApiKey and Secret out of the attribute and call to GetApi() if you put them in the web.config as usual.

Any feeback would be appreciated. 

I was trying that but I have an exception, "Invalid parameter", when I try to get api instance. Do you know what is the problem? :(

Thank you so much.

Return(GiS);

Dec 4, 2009 at 10:12 PM

I have an exception, "Invalid parameter" too.

Do you have any idea what the problem is ?

Dec 6, 2009 at 3:49 AM

Facebook Developer Toolkit 3.0 – ASP.NET MVC Sample

http://blogs.claritycon.com/blogs/kevin_marshall/archive/2009/10/06/facebook-developer-toolkit-3-0-asp-net-mvc-sample.aspx

Aug 22, 2010 at 12:35 PM

Hi,

I'm trying to make that last sample work with no luck.

Facebook Developer Toolkit 3.0 – ASP.NT MVC Sample

http://blogs.claritycon.com/blogs/kevin_marshall/archive/2009/10/06/facebook-developer-toolkit-3-0-asp-net-mvc-sample.aspx

Anyone had the same problem?