Facebook C# SDK,.NET MVC,使用 facebook 应用程序 URL 将参数传递给 ActionResults

发布于 2025-01-04 16:42:11 字数 3563 浏览 1 评论 0原文

我一直在使用 facebook c# sdk 开发 .NET MVC / Azure / SQL Azure / Facebook 应用程序。

花了一段时间,但我似乎已经让用户登录了,我能够捕获使用该应用程序所需的相关数据。

在不了解应用程序是什么的情况下,用户会经历一个 3 步过程,一旦用户完成该过程,我就会向 twitter api 发送一些生成的数据以发布推文,并使用 Bit 生成一个缩短的 url。 ly API。

然后我让用户可以选择在他们的 Facebook 墙上发布内容。我可以让应用程序将正确的数据发布到用户墙上。

我面临的问题是末尾带有 id 的 URL,它会将任何单击它的人带到显示与 URL 上的 id 相关的数据的部分视图。

URL 看起来像这样 http://apps.facebook.com/myapp/?id=1 我有两个公共 ActionResult Index(string id) ,一个带有参数(POST) &一个没有(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();
    }

在使用JQuery的索引页面上我这样做,

'

$(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{


    }

});

问题是将参数从facebook画布传递到actionresult

这个facebook应用程序url http://apps.facebook.com/myapp/<<这将是路线 /Home/Index

映射到类似的东西 http://myapp.cloudapp.net/<<这将是路线 /Home/Index

所以我假设 http://apps.facebook.com/myapp http://myapp.cloudapp.net/?id=1

/?id=1 映射到我的 Global.asax 页面中的

我有`

  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
        );

    }

任何人都可以帮助我或建议使用以下命令将参数传递给 MVC 控制器操作结果Facebook 画布 url 的 & facebook c# sdk。

对于我的应用程序来说,能够根据传递的 id 将用户定向到特定视图/部分视图非常重要。

我以正确的方式处理这件事吗?任何人都可以看到我的方法中的缺陷吗?他们是否缺少或没有做某些事情。

最好的问候

帕特里克

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

靑春怀旧 2025-01-11 16:42:11

我的处理方式正确吗?有人能看出我的缺陷吗?
接近,是他们缺少或没有做的事情。

您似乎正在尝试从 http://apps.facebook.com/myapphttp://myapp.cloudapp.net/ 执行 AJAX 请求,这显然是由于内置浏览器的同源策略限制,这是不可能的。它基本上可以阻止您发送跨域 AJAX 请求。这是一个指南,其中涵盖了一些可能的解决方法。

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.

You seem to be attempting to perform an AJAX request from http://apps.facebook.com/myapp to http://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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文