0

背景
我一直在使用的Visual Studio 2013 Express的只是創建了一個MVC網站(如應用程序)使用「個人用戶帳戶」選項從默認MVC模板中選擇Web。我已經創建了視圖,以適合帳戶模型並添加兩個字段(「用戶名」和「出生日期」)(使用Microsoft的教程)。 注意:從visual studio在localhost上運行時,一切正常。ASP.NET MVC數據庫無法創建/上傳System.ComponentModel.Win32Exception:系統找不到指定的文件

的問題
當我上傳網站my server,我得到找不到文件錯誤(我認爲這是不被人發現的帳戶數據庫)。當我上傳它時(我通過文件系統方法執行此操作),AppData文件夾中沒有任何內容,但VS中也沒有,它位於服務器資源管理器中。如果我嘗試對活動服務器上的帳戶進行任何操作(例如,註冊用戶),我會得到以下堆棧跟蹤。

堆棧跟蹤
The Stack Trace
注意:您可以在spiderhouse.org看到自己這一點,如果你註冊一個用戶;我已經提供了完整的錯誤細節並處於當前狀態(在編寫本文時),不會保存任何信息。

身份模型

using System; 
using System.Security.Claims; 
using System.Threading.Tasks; 
using Microsoft.AspNet.Identity; 
using Microsoft.AspNet.Identity.EntityFramework; 

namespace Spiderhouse.Models 
{ 
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more. 
    public class ApplicationUser : IdentityUser 
    { 
     public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) 
     { 
      // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
      var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
      // Add custom user claims here 
      return userIdentity; 
     } 
     public override string UserName { get; set; } 
     public DateTime BirthDate { get; set; } 
    } 

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
    { 
     public ApplicationDbContext() 
      : base("DefaultConnection", throwIfV1Schema: false) 
     { 
     } 

     public static ApplicationDbContext Create() 
     { 
      return new ApplicationDbContext(); 
     } 
    } 
} 

的賬戶控制器

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Security.Claims; 
using System.Threading.Tasks; 
using System.Web; 
using System.Web.Mvc; 
using Microsoft.AspNet.Identity; 
using Microsoft.AspNet.Identity.EntityFramework; 
using Microsoft.AspNet.Identity.Owin; 
using Microsoft.Owin.Security; 
using Owin; 
using Spiderhouse.Models; 

    namespace Spiderhouse.Controllers 
    { 
    [Authorize] 
    public class AccountController : Controller 
    { 
     private ApplicationUserManager _userManager; 

     public AccountController() 
     { 
     } 

     public AccountController(ApplicationUserManager userManager) 
     { 
      UserManager = userManager; 
     } 

     public ApplicationUserManager UserManager { 
      get 
      { 
       return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
      } 
      private set 
      { 
       _userManager = value; 
      } 
     } 

     // 
     // GET: /Account/Login 
     [AllowAnonymous] 
     public ActionResult Login(string returnUrl) 
     { 
      ViewBag.ReturnUrl = returnUrl; 
      return View(); 
     } 

     // 
     // POST: /Account/Login 
     [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
     { 
      if (ModelState.IsValid) 
      { 
       var user = await UserManager.FindAsync(model.UserName, model.Password); 
       if (user != null) 
       { 
        await SignInAsync(user, model.RememberMe); 
        return RedirectToLocal(returnUrl); 
       } 
       else 
       { 
        ModelState.AddModelError("", "Invalid username or password."); 
       } 
      } 

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

     // 
     // GET: /Account/Register 
     [AllowAnonymous] 
     public ActionResult Register() 
     { 
      return View(); 
     } 

     // 
     // POST: /Account/Register 
     [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Register(RegisterViewModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       var user = new ApplicationUser() { UserName = model.UserName, Email = model.Email, BirthDate = model.BirthDate }; 
       IdentityResult result = await UserManager.CreateAsync(user, model.Password); 
       if (result.Succeeded) 
       { 
        await SignInAsync(user, isPersistent: false); 

        // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 
        // Send an email with this link 
        // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); 
        // 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>"); 

        return RedirectToAction("Index", "Home"); 
       } 
       else 
       { 
        AddErrors(result); 
       } 
      } 

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

     // 
     // GET: /Account/ConfirmEmail 
     [AllowAnonymous] 
     public async Task<ActionResult> ConfirmEmail(string userId, string code) 
     { 
      if (userId == null || code == null) 
      { 
       return View("Error"); 
      } 

      IdentityResult result = await UserManager.ConfirmEmailAsync(userId, code); 
      if (result.Succeeded) 
      { 
       return View("ConfirmEmail"); 
      } 
      else 
      { 
       AddErrors(result); 
       return View(); 
      } 
     } 

     // 
     // GET: /Account/ForgotPassword 
     [AllowAnonymous] 
     public ActionResult ForgotPassword() 
     { 
      return View(); 
     } 

     // 
     // POST: /Account/ForgotPassword 
     [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       var user = await UserManager.FindByNameAsync(model.Email); 
       if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id))) 
       { 
        ModelState.AddModelError("", "The user either does not exist or is not confirmed."); 
        return View(); 
       } 

       // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 
       // Send an email with this link 
       // string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id); 
       // var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);  
       // await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>"); 
       // return RedirectToAction("ForgotPasswordConfirmation", "Account"); 
      } 

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

     // 
     // GET: /Account/ForgotPasswordConfirmation 
     [AllowAnonymous] 
     public ActionResult ForgotPasswordConfirmation() 
     { 
      return View(); 
     } 

     // 
     // GET: /Account/ResetPassword 
     [AllowAnonymous] 
     public ActionResult ResetPassword(string code) 
     { 
      if (code == null) 
      { 
       return View("Error"); 
      } 
      return View(); 
     } 

     // 
     // POST: /Account/ResetPassword 
     [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> ResetPassword(ResetPasswordViewModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       var user = await UserManager.FindByNameAsync(model.Email); 
       if (user == null) 
       { 
        ModelState.AddModelError("", "No user found."); 
        return View(); 
       } 
       IdentityResult result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password); 
       if (result.Succeeded) 
       { 
        return RedirectToAction("ResetPasswordConfirmation", "Account"); 
       } 
       else 
       { 
        AddErrors(result); 
        return View(); 
       } 
      } 

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

     // 
     // GET: /Account/ResetPasswordConfirmation 
     [AllowAnonymous] 
     public ActionResult ResetPasswordConfirmation() 
     { 
      return View(); 
     } 

     // 
     // POST: /Account/Disassociate 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Disassociate(string loginProvider, string providerKey) 
     { 
      ManageMessageId? message = null; 
      IdentityResult result = await UserManager.RemoveLoginAsync(User.Identity.GetUserId(), new UserLoginInfo(loginProvider, providerKey)); 
      if (result.Succeeded) 
      { 
       var user = await UserManager.FindByIdAsync(User.Identity.GetUserId()); 
       await SignInAsync(user, isPersistent: false); 
       message = ManageMessageId.RemoveLoginSuccess; 
      } 
      else 
      { 
       message = ManageMessageId.Error; 
      } 
      return RedirectToAction("Manage", new { Message = message }); 
     } 

     // 
     // GET: /Account/Manage 
     public ActionResult Manage(ManageMessageId? message) 
     { 
      ViewBag.StatusMessage = 
       message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed." 
       : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set." 
       : message == ManageMessageId.RemoveLoginSuccess ? "The external login was removed." 
       : message == ManageMessageId.Error ? "An error has occurred." 
       : ""; 
      ViewBag.HasLocalPassword = HasPassword(); 
      ViewBag.ReturnUrl = Url.Action("Manage"); 
      return View(); 
     } 

     // 
     // POST: /Account/Manage 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Manage(ManageUserViewModel model) 
     { 
      bool hasPassword = HasPassword(); 
      ViewBag.HasLocalPassword = hasPassword; 
      ViewBag.ReturnUrl = Url.Action("Manage"); 
      if (hasPassword) 
      { 
       if (ModelState.IsValid) 
       { 
        IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword); 
        if (result.Succeeded) 
        { 
         var user = await UserManager.FindByIdAsync(User.Identity.GetUserId()); 
         await SignInAsync(user, isPersistent: false); 
         return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess }); 
        } 
        else 
        { 
         AddErrors(result); 
        } 
       } 
      } 
      else 
      { 
       // User does not have a password so remove any validation errors caused by a missing OldPassword field 
       ModelState state = ModelState["OldPassword"]; 
       if (state != null) 
       { 
        state.Errors.Clear(); 
       } 

       if (ModelState.IsValid) 
       { 
        IdentityResult result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword); 
        if (result.Succeeded) 
        { 
         return RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess }); 
        } 
        else 
        { 
         AddErrors(result); 
        } 
       } 
      } 

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

     // 
     // POST: /Account/ExternalLogin 
     [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public ActionResult ExternalLogin(string provider, string returnUrl) 
     { 
      // Request a redirect to the external login provider 
      return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl })); 
     } 

     // 
     // GET: /Account/ExternalLoginCallback 
     [AllowAnonymous] 
     public async Task<ActionResult> ExternalLoginCallback(string returnUrl) 
     { 
      var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); 
      if (loginInfo == null) 
      { 
       return RedirectToAction("Login"); 
      } 

      // Sign in the user with this external login provider if the user already has a login 
      var user = await UserManager.FindAsync(loginInfo.Login); 
      if (user != null) 
      { 
       await SignInAsync(user, isPersistent: false); 
       return RedirectToLocal(returnUrl); 
      } 
      else 
      { 
       // If the user does not have an account, then prompt the user to create an account 
       ViewBag.ReturnUrl = returnUrl; 
       ViewBag.LoginProvider = loginInfo.Login.LoginProvider; 
       return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email }); 
      } 
     } 

     // 
     // POST: /Account/LinkLogin 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult LinkLogin(string provider) 
     { 
      // Request a redirect to the external login provider to link a login for the current user 
      return new ChallengeResult(provider, Url.Action("LinkLoginCallback", "Account"), User.Identity.GetUserId()); 
     } 

     // 
     // GET: /Account/LinkLoginCallback 
     public async Task<ActionResult> LinkLoginCallback() 
     { 
      var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(XsrfKey, User.Identity.GetUserId()); 
      if (loginInfo == null) 
      { 
       return RedirectToAction("Manage", new { Message = ManageMessageId.Error }); 
      } 
      IdentityResult result = await UserManager.AddLoginAsync(User.Identity.GetUserId(), loginInfo.Login); 
      if (result.Succeeded) 
      { 
       return RedirectToAction("Manage"); 
      } 
      return RedirectToAction("Manage", new { Message = ManageMessageId.Error }); 
     } 

     // 
     // POST: /Account/ExternalLoginConfirmation 
     [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) 
     { 
      if (User.Identity.IsAuthenticated) 
      { 
       return RedirectToAction("Manage"); 
      } 

      if (ModelState.IsValid) 
      { 
       // Get the information about the user from the external login provider 
       var info = await AuthenticationManager.GetExternalLoginInfoAsync(); 
       if (info == null) 
       { 
        return View("ExternalLoginFailure"); 
       } 
       var user = new ApplicationUser() { UserName = model.Email, Email = model.Email }; 
       IdentityResult result = await UserManager.CreateAsync(user); 
       if (result.Succeeded) 
       { 
        result = await UserManager.AddLoginAsync(user.Id, info.Login); 
        if (result.Succeeded) 
        { 
         await SignInAsync(user, isPersistent: false); 

         // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 
         // Send an email with this link 
         // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); 
         // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); 
         // SendEmail(user.Email, callbackUrl, "Confirm your account", "Please confirm your account by clicking this link"); 

         return RedirectToLocal(returnUrl); 
        } 
       } 
       AddErrors(result); 
      } 

      ViewBag.ReturnUrl = returnUrl; 
      return View(model); 
     } 

     // 
     // POST: /Account/LogOff 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult LogOff() 
     { 
      AuthenticationManager.SignOut(); 
      return RedirectToAction("Index", "Home"); 
     } 

     // 
     // GET: /Account/ExternalLoginFailure 
     [AllowAnonymous] 
     public ActionResult ExternalLoginFailure() 
     { 
      return View(); 
     } 

     [ChildActionOnly] 
     public ActionResult RemoveAccountList() 
     { 
      var linkedAccounts = UserManager.GetLogins(User.Identity.GetUserId()); 
      ViewBag.ShowRemoveButton = HasPassword() || linkedAccounts.Count > 1; 
      return (ActionResult)PartialView("_RemoveAccountPartial", linkedAccounts); 
     } 

     protected override void Dispose(bool disposing) 
     { 
      if (disposing && UserManager != null) 
      { 
       UserManager.Dispose(); 
       UserManager = null; 
      } 
      base.Dispose(disposing); 
     } 

     #region Helpers 
     // Used for XSRF protection when adding external logins 
     private const string XsrfKey = "XsrfId"; 

     private IAuthenticationManager AuthenticationManager 
     { 
      get 
      { 
       return HttpContext.GetOwinContext().Authentication; 
      } 
     } 

     private async Task SignInAsync(ApplicationUser user, bool isPersistent) 
     { 
      AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); 
      AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, await user.GenerateUserIdentityAsync(UserManager)); 
     } 

     private void AddErrors(IdentityResult result) 
     { 
      foreach (var error in result.Errors) 
      { 
       ModelState.AddModelError("", error); 
      } 
     } 

     private bool HasPassword() 
     { 
      var user = UserManager.FindById(User.Identity.GetUserId()); 
      if (user != null) 
      { 
       return user.PasswordHash != null; 
      } 
      return false; 
     } 

     private void SendEmail(string email, string callbackUrl, string subject, string message) 
     { 
      // For information on sending mail, please visit http://go.microsoft.com/fwlink/?LinkID=320771 
     } 

     public enum ManageMessageId 
     { 
      ChangePasswordSuccess, 
      SetPasswordSuccess, 
      RemoveLoginSuccess, 
      Error 
     } 

     private ActionResult RedirectToLocal(string returnUrl) 
     { 
      if (Url.IsLocalUrl(returnUrl)) 
      { 
       return Redirect(returnUrl); 
      } 
      else 
      { 
       return RedirectToAction("Index", "Home"); 
      } 
     } 

     private class ChallengeResult : HttpUnauthorizedResult 
     { 
      public ChallengeResult(string provider, string redirectUri) : this(provider, redirectUri, null) 
      { 
      } 

      public ChallengeResult(string provider, string redirectUri, string userId) 
      { 
       LoginProvider = provider; 
       RedirectUri = redirectUri; 
       UserId = userId; 
      } 

      public string LoginProvider { get; set; } 
      public string RedirectUri { get; set; } 
      public string UserId { get; set; } 

      public override void ExecuteResult(ControllerContext context) 
      { 
       var properties = new AuthenticationProperties() { RedirectUri = RedirectUri }; 
       if (UserId != null) 
       { 
        properties.Dictionary[XsrfKey] = UserId; 
       } 
       context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider); 
      } 
     } 
     #endregion 
    } 
} 

連接字符串

<connectionStrings> 
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-Spiderhouse-20141120093225.mdf;Initial Catalog=aspnet-Spiderhouse-20141120093225;Integrated Security=True" providerName="System.Data.SqlClient" /> 
</connectionStrings> 

回答

1

您提到的例外情況的第一行是說您沒有在prod服務器上安裝SQL Express。

SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)

顯然,你需要有一個安裝在爲了使您的應用程序與你放出來的.mdf文件服務器上的工作。我假設當你說你「在App_Data文件夾中看不到任何東西,但是你在服務器資源管理器中」時,你正在通過Visual Studio解決方案資源管理器查看App_Data文件夾,請更正?如果這是真的,我也沒有看到任何東西,但實際上去文件資源管理器中的目錄顯示mvc網站創建的.mdf和.ldf文件。如果您確實在服務器上安裝了SQL Express,則可能忘記將這些文件放在Web服務器上的正確位置。

在我的項目中,我選擇使用SQL Server的完整遠程安裝,在這種情況下,我需要更改連接字符串指向的位置,但是因爲您在該信息中包含了該信息,所以我認爲您知道這一點。

+0

我在項目文件夾(本地)中看到'.mdf'文件,但不在本地發佈文件夾中(它被上傳到服務器)。關於連接字符串,我使用默認的字符串。我不確定我是否應該,我只知道他們是相關的。就SQL Express的安裝而言,我現在要檢查一下,當我知道時我會發表評論。 – samdy1 2014-11-24 19:32:02

+0

那麼,我正在使用電子託管,我似乎無法看到對SQL Server Express的任何引用,所以我已經通過電子郵件發送給他們。 – samdy1 2014-11-24 19:43:31

+0

當你說你正在使用電子託管這是否意味着你無法訪問iis管理界面? – SaltpeterBoom 2014-11-24 23:25:46

相關問題