首页 > 解决方案 > Application deployed on server won't work as on local machine

问题描述

I have an application which I developed using c# and asp.net mvc (MVC5). I used the publishing tool of visual studio 2017 to publish a release package of the app which I could deploy on a server.
The app principally has two functionalities, one to register students and another to register their notes.
The one to register students works fine and I can do everything related to that (editing, deleting, etc...)
When I try to register a note for a particular student, I get an error. No matter what I do, I can't register notes.
I got an "Error.cshtml" view in my app and it's that view which is returned to me every time I try registering a note for a student.
The most strange issue is that on my local machine everything works fine (I can register both students and their notes). But once I deploy the same working version on the server, the notes functionnality does not. Below are some portions of code:

Create View :

    <div class="form-group" id="id1">
    @Html.LabelFor(model => model.NotePremierTrimestre, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.NotePremierTrimestre, new { htmlAttributes = new { @class = "form-control", @id = "id01" } })
        @Html.ValidationMessageFor(model => model.NotePremierTrimestre, "", new { @class = "text-danger" })
    </div>
</div>

Resultats controller :

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Save([Bind(Include = "IdResultat,NotePremierTrimestre,NoteDeuxiemeTrimestre,NoteTroixiemeTrimestre,NotePremierSemestre,NoteDeuxiemeSemestre,MoyenneGenerale,Mention,MoyenneCEPE,MoyenneFSLC,MoyenneBEPC,MoyenneCAP,MoyenneGCEO,MoyenneProbatoire,MoyenneBACC,MoyenneGCEA,MoyenneBTS,MoyenneDUT,MoyenneHND,MoyLicence,MoyMasterPro,MoyMasterRecherche,IdApprennant,IdAnnee")] Resultat resultat, [Bind(Include = "IdNoteDiscipline,Note,IdDiscipline,IdResultat")] NoteDiscipline noteDiscipline, string examenOfficiel)
    {
        if (ModelState.IsValid)
        {
            if (resultat.NotePremierTrimestre != null & resultat.NoteDeuxiemeTrimestre != null & resultat.NoteTroixiemeTrimestre != null)
            {
                resultat.MoyenneGenerale = (resultat.NotePremierTrimestre + resultat.NoteDeuxiemeTrimestre + resultat.NoteTroixiemeTrimestre) / 3;
            }
            else if (resultat.NotePremierSemestre != null & resultat.NoteDeuxiemeSemestre != null)
            {
                resultat.MoyenneGenerale = (resultat.NotePremierSemestre + resultat.NoteDeuxiemeSemestre) / 2;
            }
            else
            {
                resultat.MoyenneGenerale = 0;
            }

            if (examenOfficiel == "BEPC")
            {
                resultat.MoyenneBEPC = resultat.MoyenneCEPE;
            }
            else if (examenOfficiel == "CAP")
            {
                resultat.MoyenneCAP = resultat.MoyenneCEPE;
            }
            else if (examenOfficiel == "GCE O'Level")
            {
                resultat.MoyenneGCEO = resultat.MoyenneCEPE;
            }
            else if (examenOfficiel == "Probatoire")
            {
                resultat.MoyenneProbatoire = resultat.MoyenneCEPE;
            }
            else if (examenOfficiel == "Baccalauréat")
            {
                resultat.MoyenneBACC = resultat.MoyenneCEPE;
            }
            else if (examenOfficiel == "GCE A'Level")
            {
                resultat.MoyenneGCEA = resultat.MoyenneCEPE;
            }
            else if (examenOfficiel == "BTS")
            {
                resultat.MoyenneBTS = resultat.MoyenneCEPE;
            }
            else if (examenOfficiel == "Licence")
            {
                resultat.MoyLicence = resultat.MoyenneCEPE;
            }
            else if (examenOfficiel == "Master Professionnel")
            {
                resultat.MoyMasterPro = resultat.MoyenneCEPE;
            }
            else if (examenOfficiel == "Master Recherche")
            {
                resultat.MoyMasterRecherche = resultat.MoyenneCEPE;
            }
            else if (examenOfficiel == "HND")
            {
                resultat.MoyenneHND = resultat.MoyenneCEPE;
            }
            else
            {

            }

            //Vérification de la base de données pour voir si un apprenant avec cette note a déjà été enregistré
            //Resultat result2 = null;
            //try
            //{
            //    result2 = db.Resultats.Where(b => b.IdApprennant == resultat.IdApprennant && b.IdAnnee == resultat.IdAnnee).First();
            //} catch
            //{

            //}
            //if (result2 == null)
            //{
            db.Resultats.Add(resultat);

            db.SaveChanges();
            int id = resultat.IdResultat;
            noteDiscipline.IdResultat = id;

            db.NoteDisciplines.Add(noteDiscipline);
            db.SaveChanges();
            return RedirectToAction("Index");
            //}
        }

        ViewBag.IdAnnee = new SelectList(db.AnneeAcademiques, "IdAnnee", "Intitule", resultat.IdAnnee);
        ViewBag.IdApprennant = new SelectList(db.Apprenants, "IdApprenant", "Code", resultat.IdApprennant);
        //return View("Error");

        return View(resultat);
    }

With the above code I expect to send some marks from the view to the controller and to be redirected to an "Index" view or else to the same "Create" view. This works fine on my machine and I have this same code on the server, but once I type some marks and hit the register button, I have the "Error" view which is thrown back. The other strange thing is that the line of code which returns the "Error" View has been commented, but it still shows up in the server.

标签: c#asp.net-mvc

解决方案


如果您已将包托管到 IIS 服务器,请转到应用程序的基本设置并将物理路径更改为源代码而不是构建包。然后从 IIS 运行您的应用程序,并将调试器保留在 Visual Studio 的应用程序中。通过这种方式,您将能够找出从服务器运行应用程序时可能出现的问题。


推荐阅读