I been working on a .NET MVC / Azure / SQL Azure / Facebook app using the facebook c# sdk.
Took a while but I seem to have it logging users in, im able to capture the relevant data needed to use the app.
Without going into what the app is , a user goes through a 3 step process , once the user gets to the end of the process I fire off some generated data to twitter api to post a tweet and also generate a shortened url using the Bit.ly api.
I then give the user the option to post to their facebook wall. I can get the app posting with the right data to the users wall ok.
The problem I face is the URL with the id on the end that is suppose to take any person who clicks it to a partial view displaying relevant data to the id on the URL.
URL looks like this http: //apps.facebook.com/myapp/?id=1
I have two public ActionResult Index(string id) , one with a parameter(POST) & one without(GET)
`
[Authorize]
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index()
{
ViewData["Message"] = "Welcome to The My App Website!";
ViewData["id"] = "null";
try
{
var facebookId = long.Parse(User.Identity.Name);
var user = InMemoryUserStore.Get(facebookId);
var client = new FacebookClient(user.AccessToken);
dynamic me = client.Get("me");
ViewData["accessToken"] = user.AccessToken.ToString();
ViewData["FirstName"] = me.first_name;
ViewData["LastName"] = me.last_name;
ViewData["Email"] = me.email;
ViewData["uid"] = facebookId.ToString();
}
catch (Exception)
{
throw;
}
return View();
}
`
[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(string id)
{
ViewData["Message"] = "Welcome to My App!";
ViewData["id"] = id;
try
{
var facebookId = long.Parse(User.Identity.Name);
var user = InMemoryUserStore.Get(facebookId);
var client = new FacebookClient(user.AccessToken);
dynamic me = client.Get("me");
ViewData["accessToken"] = user.AccessToken.ToString();
ViewData["FirstName"] = me.first_name;
ViewData["LastName"] = me.last_name;
ViewData["Email"] = me.email;
ViewData["uid"] = facebookId.ToString();
}
catch (Exception)
{
throw;
}
return View();
}
On the index page using JQuery I do this,
‘
$(document).ready(function () {
var str = "@ViewBag.id";
if(str != "null" || str != null){
$('#imgLoading').css("display", "inline");
$('#side').css('display', 'none');
$('#contentDiv').html("");
$.get('home/GetUniqueView/?id=' + str, function (data6) {
$('#contentDiv').html(data6);
$('#imgLoading').css("display", "none");
});
}else{
}
});
The problem is passing parameters to the actionresult from the facebook canvas
This facebook app url
http: //apps.facebook.com/myapp/ << this would be route /Home/Index
maps to something like this
http: //myapp.cloudapp.net/ << this would be route /Home/Index
so im assuming http://apps.facebook.com/myapp/?id=1 maps to http: //myapp.cloudapp.net/?id=1
in my Global.asax page I have
`
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Can anyone help me or advise on passing parameters to MVC Controller Action Results with the Facebook canvas url’s & facebook c# sdk.
Its important for my app to be able to direct users to a specific view/partial view based on passed id.
Am I going about this the right way, can anyone see a flaw in my approach and is their something im missing or not doing.
best regards
Patrick
You seem to be attempting to perform an AJAX request from
http://apps.facebook.com/myapptohttp://myapp.cloudapp.net/which obviously is impossible due to the same origin policy restriction built-in browsers. It basically prevents you from sending cross domain AJAX requests. Here’s a guide which covers some possible workarounds.