Question

My mobile web site allows users to send an AppRequest to their Facebook friends. This is working.

When the friend accepts the AppRequest, Facebook sends the friend to my web site. This too is working.

My web site is an ASP.Net MVC 4 application. I am trying to get my routes to recognize the incoming AppRequest acceptance but I can't figure out how to do it.

Facebook is sending the friends to my site using this URL:

http://www.example.com/?ref=notif&code=abcdefg&fb_source=notification

This keeps getting routed to Home/Index despite my attempts to map the route to a custom controller and action. Here is what I have done so far that has failed to work:

Registered Routes:

routes.MapRoute(
   name: "FacebookAppRequest",
   url: "{ref}/{code}/{fb_source}",   //This should match the URL above
   defaults: new { controller = "Facebook", action ="FBAppRequestHandler"}
);
routes.MapRoute(
   name: "Default",
   url: "{controller}/{action}/{id}",
   defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Controller:

public class FacebookController : Controller
{
   public FacebookController() {}

   public ActionResult FBAppRequestHandler(
      [Bind(Prefix = "ref")] string fbReferal,
      [Bind(Prefix = "code")] string fbCode,
      [Bind(Prefix = "fb_source")] string fbSource)
   {
      //Do some stuff with fbReferal, fbCode and fbSource

      return View();
   }
Was it helpful?

Solution

The ref the code and the fb_source are passed as query string parameters. They are not part of the route. So you cannot possibly expect that {ref}/{code}/{fb_source} would match your custom route. That would have been the case if the request looked like that:

http://www.example.com/notif/abcdefg/notification

Since the actual route looks like this (forget about query string parameters - they are not used for routing):

http://www.example.com/

all that you have here basically is the following url /. So the best you could hope here is to modify your default route so that it routes to the desired controller:

routes.MapRoute(
   name: "Default",
   url: "{controller}/{action}/{id}",
   defaults: new { controller = "Facebook", action = "FBAppRequestHandler", id = UrlParameter.Optional }
);

Now get rid of the first route - it's not necessary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top