別の ActionResult 呼び出し 'Publish' にリダイレクトする HttpPost 'Index' ActionResult があります。問題は、次の行にブレークポイントを設定したときです。
if (accTok != null && fullImgPath != null)
Ajax ActionLinkをクリックすると2回通過し、2回目のパスでしか値を取得できないため、「公開」コントローラーは常に「空白」ビューを返します。これが Ajax の投稿に関係している可能性があることはわかっていますが、最初のパスで「accTok」と「fullImgPath」の両方の値を取得して「BlurredPhoto」のパーシャルを表示できるように、この問題を修正する方法がわかりません。見る。よろしくお願いします。ありがとう。
この問題は、[HttpPost] メソッドの前に最初に起動して「Publish」にリダイレクトする @Ajax.ActionLink が原因であることがわかりました。
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string facebookUID, string facebookAccessTok)
{
string fbUID = facebookUID;
string fullPath = "";
if (fbUID != null)
{
// Request fb profile pic
var rawImg = new Bitmap(ImageHelper.requestBitmapImage(fbUID));
var processblurredImg = new Bitmap(rawImg);
var gb = new GaussianBlur();
for (int i = 0; i < 8; i++)
{
gb.ApplyInPlace(processblurredImg);
}
// Download it to local drive / server
string uploadPath = Server.MapPath("~/upload");
fullPath = uploadPath + "\\ProfilePic.png";
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
if (uploadPath != null)
ImageHelper.savePng(fullPath, processblurredImg, 500L);
}
return RedirectToAction("Publish", new { accTok = facebookAccessTok, fullImgPath = fullPath });
}
public PartialViewResult Publish( string accTok, string fullImgPath)
{
if (accTok != null && fullImgPath != null)
{
UploadPhoto(accTok, fullImgPath);
// PostToWall(accTok, fullPath);
return PartialView("BlurredPhoto");
}
return PartialView("Blank");
}
私のインデックスビュー:
@using Specsavers_Fred_Hollow.Helpers
@{
ViewBag.Title = "Home Page";
//Layout = "~/Views/Shared/_Layout.cshtml";
}
<script type="text/javascript">
var uid = 0;
var accesstoken = '';
function grantPermission() {
window.FB.login(function (response) {
if (response.authResponse) {
uid = response.authResponse.userID;
accesstoken = response.authResponse.accessToken;
var postData = { facebookUID: uid, facebookAccessTok: accesstoken };
$.ajax({
url: '@Url.Action("Index","Tab")',
type: 'POST',
data: postData,
dataType: 'json',
success: function (response) {
// process the results from the controller action
// window.location.href = response.Url;
}
});
} else {
console.log('User cancelled login or did not fully authorize.');
alert('User cancelled login');
}
}, { scope: 'publish_stream' });
};
// Init the SDK upon load
window.fbAsyncInit = function() {
FB.init({
appId: '@FacebookHelper.FacebookAppId', // App ID
channelUrl: '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true // parse XFBML
});
// Load the SDK Asynchronously
(function(d) {
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {
return;
}
js = d.createElement('script');
js.id = id;
js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
// listen for and handle auth.statusChange events
FB.Event.subscribe('auth.statusChange', function(response) {
if (response.authResponse) {
// user has auth'd your app and is logged into Facebook
FB.api('/me', function(me) {
if (me.name) {
document.getElementById('auth-displayname').innerHTML = me.name;
}
});
document.getElementById('auth-loggedout').style.display = 'none';
document.getElementById('auth-loggedin').style.display = 'block';
} else {
// user has not auth'd your app, or is not logged into Facebook
document.getElementById('auth-loggedout').style.display = 'block';
document.getElementById('auth-loggedin').style.display = 'none';
}
});
$("#auth-loginlink").click(function() { grantPermission(); });
};
</script>
<html>
<head>
<title>Facebook Login Authentication Example</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"> </script>
</head>
<body>
<h1>Facebook Login Authentication</h1>
<br/>
<div id="auth-status">
@*<a href="#" id="auth-loginlink">Login</a><br/>*@
@*<div class="fb-login-button" id="auth-loginlink"></div>*@
</div>
@Ajax.ActionLink("Proceed", "Publish", "Tab",
new { id = "auth-loginlink" },
new AjaxOptions{UpdateTargetId = "DynamicContainer",
InsertionMode=InsertionMode.Replace,
HttpMethod="POST",
OnSuccess = "grantPermission()"
})
<br/><br/>
<div id="DynamicContainer" style="border: 1px solid #C0C0C0; padding: 1px; width: 500px; height: 400px"></div>
</body>
</html>