I am using a partial view to render the users total number of items they have in my application. I currently have this on my master page:
<%= Html.Action("MyItemsTotal", "Home") %>
In my home controller i have this action method:
[ChildActionOnly]
public ActionResult MyItemsTotal()
{
FacebookApp app = new FacebookApp();
ViewData["count"] = "0";
int count = 0;
if (app.Session != null)
count = (from i in db.Items
where i.UserID == app.Session.UserId &&
i.Status != "D"
select i).Count();
if (count > 0)
ViewData["count"] = count.ToString("#,#");
return PartialView();
}
The problem is that app.Session is null even though prior to loading the page I am using Url.CanvasAction so my url in the browser looks like this:
http://apps.facebook.com/mysite/Home
I thought that the users session was maintained as long as I s开发者_StackOverflow中文版tay within the facebook domain (i.e. apps.facebook.com) ... ?
I was using Html.Action before but since installing the latest version of the SDK (v 4.1.1) it is no longer working ... has something changed in the latest version to prevent Html.Action from working now???
Is there an equivalent to Html.Action for example Html.CanvasAction that I can use for rendering partial views? (Obviously there isn't one there ... )
Regards, Rob
There are are a few classes in the Facebook.Web.Mvc project that add extension methods to the HtmlHelper, UrlHelper, and Controller classes. You will find extensions like:
Html.CanvasAction Url.CanvasAction this.CanvasRedirect (in the controller, it is an extension so you have to use 'this')
We have handled all the same overloads that are provided with the MVC Framework for each of the methods.
Purely by accident I think I've groaked how the SDK works!
A side note to the developers of the SDK, you've done a wonderful job, but try explaining things a bit better... Use pictures and data flows so that people can get to using yr wonderful sdk a lot quicker!
If in your Facebook Settings you have this setting enabled:
- Post to Canvas
As per the documentation it states that facebook will Post the session data and other information to your end-point as form fields. e.g.
<input "signed-request" value="...
what they don't say is that this only works when you in their domain, that is:
http://apps.facebook.com/<yourAppNameHere>
it won't post anything outside of their domain e.g:
http://www.<yourAppNameHere>.com
Now you maybe thinking, well dah! But wait there is more!
If your like me and your using Asp.Net MVC with Iframe Canvas enabled as your application settings within facebook then when you try and use helper methods for rendering partial pages (effectively user-controls in webforms) and you include this on your page:
<%= Html.Action("MyItemsTotal", "Home") %>
Unfortunately in the MyItemsTotal action method in the Home controller you will not received session data. Why is that you ask ... because Asp.Net MVC is using a Get Request!
And hence your session data no longer exists in that context. Your effectively calling your own domain.
In my situation I'm only wanting to retrieve the userId so I could do something like this instead:
<%= Html.Action("MyItemsTotal", "Home", new RouteValueDictionary { { "UserID", FbApp.Session.UserId }}) %>
And interrogate that on return.
The better approach would be to use a session helper class to maintain the users session that FacebookApp inherits from. For all intents and purposes I can just use it in whatever way I like and it is intelligent enough to store and expose the current users information over whichever context. The question arises then, well what about users entering your site from outside of facebook, how will the new FacebookApp class determine that?
My answer to that is that it doesn't need to know which domain is calling it really, it just needs to know that it will expire in a certain amount of time if it is not being used. To maintain integrity and so fourth one would have to use the session id or whichever unique identifier facebook pass to the application.
Some food for thought :-P
精彩评论