skip to Main Content

ASP.NET MVC works fine on local machine but once deployed on Azure App Service, the pages related to a writing operation on the database do not display (pages that add data to the database and pages that update data in the database).

Here’s an exmple of code of one controller that inserts data:

public ActionResult Create()
{
    ViewBag.nbEnt = db.Entrepots.Where(e => e.active == true).Count();
    return View();
}
  
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,entrName,entrAdres,LocaliteId,entrFreePlace,entrBusyPlace,entrTotalPlace,active")] Entrepot entrepot)
{
    if (ModelState.IsValid)
    {
        entrepot.active = true;
        db.Entrepots.Add(entrepot);

        db.SaveChanges();

        TempData["action"] = "add";

        return RedirectToAction("Index");
    }

    ViewBag.LocaliteId = new SelectList(db.Localites, "Id", "locTown", entrepot.LocaliteId);

    return View(entrepot);
} 

Here’s an example of an update:

public ActionResult Edit(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    Entrepot entrepot = db.Entrepots.Find(id);

    if (entrepot == null)
    {
        return HttpNotFound();
    }

    return View(entrepot);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,entrName,entrAdres,LocaliteId,entrFreePlace,entrBusyPlace,entrTotalPlace,active")] Entrepot entrepot)
{
    if (ModelState.IsValid)
    {
        db.Entry(entrepot).State = EntityState.Modified;
        db.SaveChanges();

        return RedirectToAction("Index");
    }

    ViewBag.LocaliteId = new SelectList(db.Localites, "Id", "locTown", entrepot.LocaliteId);

    return View(entrepot);
}

Any clue or advice on this?

Thanks.

I’ve tried the debugger linked to Visual Studio but that hasn’t helped me.

2

Answers


  1. Chosen as BEST ANSWER

    I managed to solve the problem by removing from the View the anti forgery token

    @Html.AntiForgeryToken()

    and also removed it from the controller :

            [HttpPost]
       //   [ValidateAntiForgeryToken]
            public ActionResult Edit([Bind(Include = "Id,entrName,entrAdres,LocaliteId,entrFreePlace,entrBusyPlace,entrTotalPlace,active")] Entrepot entrepot)
            {
            if (ModelState.IsValid)...
    

    now the code works fine, thanks to you all for help and support, that helped me to test my code with your advices.


  2. I tried the simple ASP. NET MVC Framework 4.8 with SQL Server and deployed it to Azure.

    Ensure that your database schema is properly migrated when deploying to Azure. If you are using Entity Framework Code First migrations, make sure that your migrations are applied correctly to the Azure SQL Database.

    Make Sure that your Azure SQL Database is properly configured and accessible from your Azure App Service.

    This is my controller, used to create and update the data in my database.

    EntrepotsController:

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Mvc;
    using WebApplication4.Models;
    
    namespace WebApplication4.Controllers
    {
        public class EntrepotsController : Controller
        {
            private EntrepotDbContext db = new EntrepotDbContext();
            public ActionResult Index()
            {
                return View(db.Entrepots.ToList());
            }
            public ActionResult Details(int? id)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Entrepot entrepot = db.Entrepots.Find(id);
                if (entrepot == null)
                {
                    return HttpNotFound();
                }
                return View(entrepot);
            }
            public ActionResult Create()
            {
                return View();
            }
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create([Bind(Include = "Id,entrName,entrAdres,LocaliteId,active")] Entrepot entrepot)
            {
                if (ModelState.IsValid)
                {
                    db.Entrepots.Add(entrepot);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(entrepot);
            }
            public ActionResult Edit(int? id)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Entrepot entrepot = db.Entrepots.Find(id);
                if (entrepot == null)
                {
                    return HttpNotFound();
                }
                return View(entrepot);
            }
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Edit([Bind(Include = "Id,entrName,entrAdres,LocaliteId,active")] Entrepot entrepot)
            {
                if (ModelState.IsValid)
                {
                    db.Entry(entrepot).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(entrepot);
            }
            public ActionResult Delete(int? id)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Entrepot entrepot = db.Entrepots.Find(id);
                if (entrepot == null)
                {
                    return HttpNotFound();
                }
                return View(entrepot);
            }
            [HttpPost, ActionName("Delete")]
            [ValidateAntiForgeryToken]
            public ActionResult DeleteConfirmed(int id)
            {
                Entrepot entrepot = db.Entrepots.Find(id);
                db.Entrepots.Remove(entrepot);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    db.Dispose();
                }
                base.Dispose(disposing);
            }
        }
    }
    

    Below is my Azure SQL Server connection string.

    Web.Config:

     <connectionStrings>
       <add name="ConnectionString" connectionString="Server=tcp:<ServerName>.database.windows.net,1433;Initial Catalog=<DataBaseName>;Persist Security Info=False;User ID=<UserName>;Password=<Password>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient" />
     </connectionStrings>
    

    Local Output:

    enter image description here

    Before deploying a web app to Azure, if you are generating migrations using a local SQL Database ConnectionString, remove those migrations and generate new migrations using an Azure SQL Database ConnectionString.

    Azure App Service Output:

    enter image description here

    Below Is My Azure Sql Database Output:

    enter image description here

    enter image description here

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search