none
why Server.MapPath not working online? RRS feed

  • Question

  • Why Server.MapPath is not working after publishing web application?
    Is working fine when I run application local but when I published the web application it not working.

    I also want to be able to send email even if there is no attachment. 

    [HttpPost]
    public ActionResult Upload(Models.FileModel model, HttpPostedFileBase filedoc)
    {
        string filename = string.Empty;
        string path = string.Empty;
    
        if (filedoc.ContentLength > 0)
        {
               filename = Path.GetFileName(filedoc.FileName);
               path = Path.Combine(Server.MapPath("~/Docs"), filename);
               filedoc.SaveAs(path);
         }
          MailMessage _mail = new MailMessage();
          _mail.To = new MailAddress(model.to);
          _mail.From = new MailAddress(model.from, model.FirstName.Substring(0,1)+" "+model.LastName);
         Attachment attachment = new Attachment(path);
         _mail.Subject = "Testing";
         _mail.Attachments.Add(attachment);
         _mail.Body = "please find attached file.";
         SmtpClient smtp = new SmtpClient();
         smtp.Host = "mail.*****.**.**";
         smtp.Port = 25;
         smtp.UseDefaultCredentials = false;
         mtp.Credentials = new System.Net.NetworkCredential("***@*****.**.**", "*******");
         smtp.EnableSsl = false;
         smtp.Send(_mail);
    }



    Please help me.

    Thursday, August 17, 2017 8:05 PM

All replies

  • In all likelihood, MapPath is actually working and returning the correct path. I suspect that your problem is caused because in the next statement you attempt to write a file into that path. During development time, the website runs under your own account and has write permissions on the folder. But after you publish to a server, the default permissions for a web site do not allow writing into the folder structure of the web site itself.

    You could probably tweak the permissions to allow it, but it is not a good idea. Instead, don't save the HttpPostedFile into disk. Just keep it as a Stream, and then use that Stream to build the Attachment (one of the overloads of the Attachment constructor accepts a Stream instead of a Path). In this way you avoid writing to disk, gaining speed and avoiding problems such as write permissions or file name collisions.

    Thursday, August 17, 2017 8:37 PM
  • Why Server.MapPath is not working after publishing web application?
    Is working fine when I run application local but when I published the web application it not working.

    IIS Express and IIS the real one are not the same thing. Yeah it all works great on IIS Express,  and then BAM it don't work on IIS the real one when solution is deployed. You can deploy your Web application through VS to the local IIS the real one on your development machine too and code against the real IIS to avoid such issues. 

    Thursday, August 17, 2017 9:37 PM
  • Hi DA924x1,

    According to your question is more related to MVC , you could post a new thread to MVC forum for suitable support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools.If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Best Regards,
    feih-7


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 18, 2017 7:10 AM
  • Hi DA924x1,

    According to your question is more related to MVC , you could post a new thread to MVC forum for suitable support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools.If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Best Regards,
    feih-7


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    It's not a MVC issue either, but rather an IIS issue.

    Friday, August 18, 2017 7:48 PM