asp.net-mvc - 返回视图停止工作
问题描述
我修改了 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);
}
解决方案
我已经修好了。我不知道为什么,但是在发送电子邮件后保存图像让它再次工作。
如果有人知道为什么,那么我很想知道。
推荐阅读
- java - 如何使用井号显示文档
- ios - 在 iOS Swift 上发布到 Instagram 屏幕
- php - 为什么语法错误,脚本中出现意外的“回声”(T_ECHO)?
- javascript - 刷新浏览器窗口时,带有keycloak js身份验证中间件的nuxt js页面重新加载了两次?nuxtjs路由器不等到promise解决
- php - 是否可以为每个客户端使用具有多个数据库的相同 PHP 代码?
- mongodb - 架构定义
- ruby-on-rails - 在 spree 上只安装管理员而不安装管理员
- java - Java:也添加要计算的更改
- raspberry-pi - Fluidsynth 第一次在 Raspbian Lite 4.18 中不工作
- c++ - C++:如何设置函数返回值的精度?