ASP.NET MVC托管在GoDaddy中

本文关键字:GoDaddy NET MVC ASP | 更新日期: 2023-09-27 17:56:11

我一直在开发ASP。NET MVC网站,并已在GoDaddy中托管。你可以直接将查询表格发送到我正在制作的网站中的特定电子邮件,它运行得很好,表格发送没有问题,直到我托管它。每当我试图发送表格时,我都会收到这个错误:

系统。IO.DirectoryNotFoundException:找不到路径'G:''PleskVhosts''sellers.com''httpdocs''App_Data''uploads''SOS.docx'

但是,当我尝试运行未托管的项目时,表单发送仍然没有问题。为什么?这是我的控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Index(EmailFormModel model, IEnumerable<HttpPostedFileBase> files)
    {
        if (ModelState.IsValid)
        { 
            List<string> paths = new List<string>();
            foreach (var file in files)
            {
                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                    file.SaveAs(path);
                    paths.Add(path);
                }
            }
                var message = new MailMessage();
                foreach (var path in paths)
                {
                    var fileInfo = new FileInfo(path);
                    var memoryStream = new MemoryStream();
                    using (var stream = fileInfo.OpenRead())
                    {
                        stream.CopyTo(memoryStream);
                    }
                    memoryStream.Position = 0;
                    string fileName = fileInfo.Name;
                    message.Attachments.Add(new Attachment(memoryStream, fileName));
                }
                //Rest of business logic here
                string EncodedResponse = Request.Form["g-Recaptcha-Response"];
                bool IsCaptchaValid = (ReCaptcha.Validate(EncodedResponse) == "True" ? true : false);
                if (IsCaptchaValid)
                {
                    var body = "<p><b>Email From:</b> {0} ({1})</p><p><b>Subject:</b> {2} </p><p><b>Message:</b></p><p>{3}</p><p><b>Description:</b></p><p>{4}</p>";
                    message.To.Add(new MailAddress("***"));  // replace with valid value 
                    message.From = new MailAddress("***");  // replace with valid value
                    message.Subject = "(Inquire)";
                    message.Body = string.Format(body, model.FromName, model.FromEmail, model.FromSubject, model.Message, model.Desc);
                    message.IsBodyHtml = true;
                    using (var smtp = new SmtpClient())
                    {
                        var credential = new NetworkCredential
                        {
                            UserName = "***",  // replace with valid value
                            Password = "***"  // replace with valid value
                        };
                        smtp.Credentials = credential;
                        smtp.Host = "smtp.live.com";
                        smtp.Port = 587;
                        smtp.EnableSsl = true;
                        smtp.SendCompleted += (s, e) =>
                        {
                            //delete attached files
                            foreach (var path in paths)
                                System.IO.File.Delete(path);
                        };
                        await smtp.SendMailAsync(message);
                        ViewBag.Message = "Your message has been sent!";
                        ModelState.Clear();
                        return View("Index");
                    }
                } else
                {
                    TempData["recaptcha"] = "Please verify that you are not a robot!";
                }
            } return View(model);
        } 

我想问题出在路径上,但我真的不知道该怎么解决。请有人帮我。我对这种东西还不熟悉。提前谢谢。此外,当网站托管时,导航栏会变为蓝色,但在本地主机中,导航栏是黑色的。背景图片没有显示在托管网站上。请帮帮我。谢谢。

ASP.NET MVC托管在GoDaddy中

这种类型的错误发生在以下情况中

案例1:您还没有设置以编程方式/通过IIS创建文件的权限,所以您必须从您的cpanel更改权限。

案例2:文件路径可能包含特殊字符,但在您的案例中,有所有纯文本

Soln:使用file exist来检查文件是否存在,这样你就可以找出问题所在。。。。