首页 > 解决方案 > 返回视图停止工作

问题描述

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

问题是由于添加了文件处理代码,返回视图(“信息”)不再做任何事情,注册页面只是刷新。我已经浏览了代码,它仍然可以毫无问题地返回视图(“信息”)。有人知道原因/解决方案吗?我想知道这是否与注册操作是异步的有关?

这是代码:

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

标签: asp.net-mvcview

解决方案


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

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


推荐阅读