返回视图停止工作

时间:2018-06-27 16:08:57

标签: asp.net-mvc view

我已经修改了mvc5注册,以允许用户上传个人资料图像。一切正常,上传文件,正确填充数据库,并发送电子邮件确认电子邮件。

问题在于,由于添加了文件处理代码,所以return view(“ info”)不再执行任何操作,而是刷新注册页面。我已经遍历了代码,它仍然可以正常地返回return view(“ info”)。有人知道原因/解决方案吗?我想知道这是否与异步注册操作有关?

代码如下:

// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{


var userD = new UserDetails();
userD.Name = model.Name;
Regex rgx = new Regex("[^a-zA-Z0-9]");

string UserFolder = rgx.Replace(model.Email + DateTime.Now, "");
userD.UserRootFolder = UserFolder;

//create root folder
string RootPath = @"~/UserFiles/" + UserFolder + "/";

//check it doesnt already exist
if (!Directory.Exists(Server.MapPath(RootPath)))
{
//create the directory
DirectoryInfo di = Directory.CreateDirectory(Server.MapPath(RootPath));
if(!di.Exists)
{
//if the folder still doesnt exist return to view as something went wrong
//return View(model);
}
userD.UserRootFolder = RootPath;
}

HttpPostedFileBase TheFile = model.file;
if (TheFile != null)
{

string path = Server.MapPath(RootPath + model.file.FileName);
model.file.SaveAs(path);

userD.UserPictureLocation = path;
model.file.InputStream.Close();
}


var user = new ApplicationUser
{
UserName = model.Email,
Email = model.Email,
PaypalEmail=model.PaypalEmail,
UserDetails = userD

};

var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{

string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
code = System.Web.HttpUtility.UrlEncode(code);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

ViewBag.Message = "Check your email and confirm your account, you must be confirmed "
+ "before you can log in.";

return View("info");
// return RedirectToAction("Index", "Home");
}
AddErrors(result);
}

// If we got this far, something failed, redisplay form
return View(model);
}

2 个答案:

答案 0 :(得分:0)

您是否知道是否发送调试和等待电子邮件可以渲染屏幕? 答:如果是,我认为最好在数据库中注册此确认电子邮件,然后他们再次拨打电话以发送尚未发送的所有确认电子邮件。

顺便问一下,您可以共享“查看信息”的代码和路径吗?

答案 1 :(得分:0)

我已修复它。我不知道为什么,但是在发送电子邮件后保存图像使它再次工作。

如果有人知道为什么,那么我很想知道。