You’re developing a SharePoint provider hosted app and you need to do a form post to a controller action, and in that controller action you need to get a SharePoint context using the SharePointContextProvider class.
When you do this, you may find that an exception is thrown by this class because there aren’t any of the
SPxxxx query-string parameters, SharePointContextProvider needs at least the
SPHostUrl parameter to be present and if you don’t add this to your route parameters for
GET/POST, then you’ll get this exception.
You can do this quite easily by adding
SPHostUrl/SPAppWebUrl et al as ‘route parameters’ using a RouteValueDictionary to the Html.BeginForm(…) markup in your razor file…
As seen above I create the
SPxxx query-string parameters as additional route data by storing the values in the ViewBag in the controller action…
and the code for this is simple enough…[gist ca47133a6887599e72b0/]